C#实现进程管理的启动和停止实例

2019-12-26 12:27:56王冬梅

易采站长站为您分析C#实现进程管理的启动和停止方法,以操作记事本程序为例,实例分析了C#针对进程操作的基本技巧,需要的朋友可以参考下

本文实例讲述了C#实现进程管理的启动和停止方法。。具体实现方法如下:

 

 
  1. using System;  using System.Collections.Generic; 
  2. using System.ComponentModel;  using System.Data; 
  3. using System.Drawing;  using System.Text; 
  4. using System.Windows.Forms;  using System.IO; 
  5. //引用命名空间  using System.Diagnostics; 
  6. using System.Threading;  namespace StartStopProcess 
  7. {  public partial class Form1 : Form 
  8. {  int fileIndex; 
  9. string fileName = "Notepad.exe";  Process process1 = new Process(); 
  10. public Form1()  { 
  11. InitializeComponent();  //以详细列表方式显示 
  12. listView1.View = View.Details;  //参数含义:列名称,宽度(像素),水平对齐方式 
  13. listView1.Columns.Add("进程ID", 70, HorizontalAlignment.Left);  listView1.Columns.Add("进程名称", 70, HorizontalAlignment.Left); 
  14. listView1.Columns.Add("占用内存", 70, HorizontalAlignment.Left);  listView1.Columns.Add("启动时间", 70, HorizontalAlignment.Left); 
  15. listView1.Columns.Add("文件名", 280, HorizontalAlignment.Left);  } 
  16. private void buttonStart_Click(object sender, EventArgs e)  { 
  17. string argument = Application.StartupPath + "myfile" + fileIndex + ".txt";  if (File.Exists(argument)==false) 
  18. {  File.CreateText(argument); 
  19. }  //设置要启动的应用程序名称及参数 
  20. ProcessStartInfo ps = new ProcessStartInfo(fileName, argument);  ps.WindowStyle = ProcessWindowStyle.Normal; 
  21. fileIndex++;  Process p = new Process(); 
  22. p.StartInfo = ps;  p.Start(); 
  23. //等待启动完成,否则获取进程信息可能会失败  p.WaitForInputIdle();