一、知识点
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);
}
/// <summary>
/// 批处理执行命令
/// </summary>
/// <param name="commands">命令</param>
private void ExecBatCommand(string[] commands)
{
Process pro = null;
pro = new Process();
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.CreateNoWindow = true;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.OutputDataReceived += new DataReceivedEventHandler(pro_OutputDataReceived);
pro.Start();
for (int i = ; i < commands.Length; i++)
{
pro.StandardInput.WriteLine(commands[i]);
}
pro.BeginOutputReadLine();
pro.Close(); }
批处理执行命令
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 + "\r\n";
//让滚动条自动滚动到最下面
tb_result.SelectionStart = tb_result.Text.Length;
tb_result.ScrollToCaret();
}
向显示窗口追加内容