6. Process Control
本视频主要介绍 Process 控件的应用和关闭窗口的方法Kill。
1.记事本程序
添加process控件,在Startinfo中需要设置FileName属性为需要运行的程序即Notepad.exe,WorkingDirection属性设置为%system%\system32。
第一个按钮的代码:
notepadProcess.EnableRaisingEvents = true;
notepadProcess.Start();
第二个按钮的代码:
notepadProcess.Kill();
退出时出现对话框说明是进程结束,在其exit方法中添加代码:
MessageBox.Show("Notepad has been closed by user", "Process Terminated");
2.IE浏览器
添加process控件,其它不用设置。其实是在代码中设置。
第一个按钮的代码:
string programFilesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string iePath = System.IO.Path.Combine(programFilesPath, @"Internet Explorer\IExplore.exe");//这算是另外一种方法,来关联process启动的程序。
internetExplorerProcess.EnableRaisingEvents = true;
internetExplorerProcess.StartInfo.FileName = iePath;
internetExplorerProcess.Start();
第二个按钮的代码:
internetExplorerProcess.Kill();
退出时出现对话框说明是进程结束:
MessageBox.Show("Internet Explorer has been closed by user", "Process Terminated");
最后,当直接使用kill()方法时,会出现错误,因为进程中没有需要结束的进程,所以会报错,这也是kill方法的缺陷。
1.记事本程序
添加process控件,在Startinfo中需要设置FileName属性为需要运行的程序即Notepad.exe,WorkingDirection属性设置为%system%\system32。
第一个按钮的代码:
notepadProcess.EnableRaisingEvents = true;
notepadProcess.Start();
第二个按钮的代码:
notepadProcess.Kill();
退出时出现对话框说明是进程结束,在其exit方法中添加代码:
MessageBox.Show("Notepad has been closed by user", "Process Terminated");
2.IE浏览器
添加process控件,其它不用设置。其实是在代码中设置。
第一个按钮的代码:
string programFilesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string iePath = System.IO.Path.Combine(programFilesPath, @"Internet Explorer\IExplore.exe");//这算是另外一种方法,来关联process启动的程序。
internetExplorerProcess.EnableRaisingEvents = true;
internetExplorerProcess.StartInfo.FileName = iePath;
internetExplorerProcess.Start();
第二个按钮的代码:
internetExplorerProcess.Kill();
退出时出现对话框说明是进程结束:
MessageBox.Show("Internet Explorer has been closed by user", "Process Terminated");
最后,当直接使用kill()方法时,会出现错误,因为进程中没有需要结束的进程,所以会报错,这也是kill方法的缺陷。