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();
            }
    向显示窗口追加内容
  • 相关阅读:
    我的WCF之旅(1):创建一个简单的WCF程序
    网页设计中颜色的搭配
    CSS HACK:全面兼容IE6/IE7/IE8/FF的CSS HACK
    UVa 1326 Jurassic Remains
    UVa 10340 All in All
    UVa 673 Parentheses Balance
    UVa 442 Matrix Chain Multiplication
    UVa 10970 Big Chocolate
    UVa 679 Dropping Balls
    UVa 133 The Dole Queue
  • 原文地址:https://www.cnblogs.com/caoyc/p/4624315.html
Copyright © 2011-2022 走看看