• 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();
            }
    向显示窗口追加内容
  • 相关阅读:
    js计算器
    还差得远
    首个项目结束
    Web前端开发规范文档你需要知道的事
    CSS需要经过多次练习和积累才能掌握
    在Javascript中闭包(Closure)
    Web前端和网站美工
    IE中的条件注释
    计算机类期刊投稿
    忙就一个字
  • 原文地址:https://www.cnblogs.com/caoyc/p/4624315.html
Copyright © 2020-2023  润新知