zoukankan      html  css  js  c++  java
  • C# winform 安装服务

    一、知识点

    1、安装服务

    installutil HardwareScanService.exe  //安装服务
    sc config HardwareScanService type= interact type= own //允许服务于桌面交换
    Net Start HardwareScanService //启动服务

    2、卸载服务

    installutil /u HardwareScanService.exe

    二、程序界面        

        

    三、具体代码实现

            delegate void deleAppendText(string str);
            deleAppendText dat;
            public Form1()
            {
                InitializeComponent();
                dat = new deleAppendText(appendText);
            }
     1         /// <summary>
     2         /// 批处理执行命令
     3         /// </summary>
     4         /// <param name="commands">命令</param>
     5         private void ExecBatCommand(string[] commands)
     6         {
     7             Process pro = null;
     8             pro = new Process();
     9             pro.StartInfo.FileName = "cmd.exe";
    10             pro.StartInfo.UseShellExecute = false;
    11             pro.StartInfo.CreateNoWindow = true;
    12             pro.StartInfo.RedirectStandardInput = true;
    13             pro.StartInfo.RedirectStandardOutput = true;
    14             pro.StartInfo.RedirectStandardError = true;
    15             pro.OutputDataReceived += new DataReceivedEventHandler(pro_OutputDataReceived);
    16             pro.Start();
    17             for (int i = 0; i < commands.Length; i++)
    18             {
    19                 pro.StandardInput.WriteLine(commands[i]);
    20             }
    21             pro.BeginOutputReadLine();
    22             pro.Close();
    23      
    24         }
    批处理执行命令
            void pro_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                if (e.Data != null)
                {
                    this.BeginInvoke(dat, new object[] { e.Data });
                }
            }
    回显调用
            /// <summary>
            /// 安装服务
            /// </summary>
            private void install()
            {
                ExecBatCommand(new string[] { 
                    "installutil HardwareScanService.exe",
                    "sc config HardwareScanService type= interact type= own",
                    "Net Start HardwareScanService"
                });
            }
    安装服务
            private void uninstall()
            {
                ExecBatCommand(new string[] { 
                    "installutil /u HardwareScanService.exe"
                });
            }
    卸载服务
            private void appendText(string str)
            {
                tb_result.Text += str + "
    ";
                //让滚动条自动滚动到最下面
                tb_result.SelectionStart = tb_result.Text.Length;
                tb_result.ScrollToCaret();
            }
    向显示窗口追加内容
  • 相关阅读:
    第十一章 前端开发-jQuery
    第十一章 前端开发-JavaScript
    js特效 15个小demo
    开发”小米商城官网首页”(静态页面)
    浅析BFC及其作用
    python mysql curros.executemany 批量添加
    第十一章 前端开发-css
    spark读取文件机制 源码剖析
    zookeeper从入门到放弃(转载学习)
    hbase架构和读写过程
  • 原文地址:https://www.cnblogs.com/caoyc/p/4624315.html
Copyright © 2011-2022 走看看