一、知识点
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(); }