C#启动进程之Process

在程序设计中,我们经常会遇到要从当前的程序跳到另一个程序的设计需求。也就是当前进程创建另一个进程。C#提供了Process使得我们很方便的实现。

1、Process基本属性和方法

Id          //进程的Id

ProcessName      //进程的名称

PriorityClass      //进程的优先级

HandleCount      //进程句柄数

PrivateMemorySize64      //关联的进程分配的专用内存量

WorkingSet64        //工作集,关联的进程分配的物理内存量

StartInfo      //进程启动的相关属性

.

.

GetProcesses()   //获取当前系统的所有进程

Start()     //启动进程

Kill();    //强行杀死进程

.

.

例如:通过Process.GetProcesses()方法可以获取当前系统中的进程,再获取他们的相关属性值,我们可以就可以实先类似于任务管理中的进程管理了。

2、利用Process调用命令行--cmd

 1  /// <summary>
 2         /// 执行Cmd
 3         /// </summary>
 4         /// <param name="argument">cmd命令</param>
 5         /// <param name="msg">返回信息</param>
 6         /// <param name="directoryPath">路径</param>
 7         /// <param name="closed">是否关闭</param>
 8         public static void RunCmd(string argument,out string msg, string directoryPath = "",bool redirect=false)
 9         {
10             msg = string.Empty;
11             Process process = new Process();
12             ProcessStartInfo startInfo=new ProcessStartInfo();
13             startInfo.FileName = "cmd.exe";
14             startInfo.Arguments=redirect?@"/c "+argument:@"/k "+argument;
15             startInfo.UseShellExecute=false;                        //是否需要启动windows shell
16             startInfo.CreateNoWindow=false;
17             startInfo.RedirectStandardError=redirect;    //是否重定向错误
18             startInfo.RedirectStandardInput = redirect;    //是否重定向输入   是则不能在cmd命令行中输入
19             startInfo.RedirectStandardOutput = redirect;      //是否重定向输出,是则不会在cmd命令行中输出
20             startInfo.WorkingDirectory=directoryPath;       //指定当前命令所在文件位置,
21             process.StartInfo = startInfo;
22             process.Start();
23             if (redirect)
24             {
25                 process.StandardInput.Close();
26                 msg = process.StandardOutput.ReadToEnd();  //在重定向输出时才能获取
27             }
28             //else
29             //{
30             //    process.WaitForExit();//等待进程退出
31             //}
32         }

这里需要注意三点,1、是重定向,重定向为true时方可获取返回值,并且要求命令行退出。2、WorkingDirectory,在调用命令行时需注意指定工作目录,默认都是当前程序启动目录。(特别是那些需要在指定目录运行的注册exe,当然他算exe范畴了,但会弹出命令行显示进度,所以再此提一下)3、当程序为控制台时,系统为默认在控制台上执行命令行命令。

3、启动exe

 1  /// <summary>
 2         /// 启动exe
 3         /// </summary>
 4         /// <param name="filePath">程序路径</param>
 5         /// <param name="argument">参数</param>
 6         /// <param name="waitTime">等待时间,毫秒计</param>
 7         public void RunExe(string filePath,string argument,int waitTime=0)
 8         {
 9             if (string.IsNullOrEmpty(filePath))
10             {
11                 throw new Exception("filePath is empty");
12             }
13             if (!File.Exists(filePath))
14             {
15                 throw new Exception(filePath + " is not exist");
16             }
17             string directory = Path.GetDirectoryName(filePath);
18 
19             try
20             {
21                 Process p = new Process();
22                 p.StartInfo.FileName = filePath;
23                 p.StartInfo.WorkingDirectory = directory;
24                 p.StartInfo.Arguments = argument;
25                 p.StartInfo.ErrorDialog = false;
26                 //p.StartInfo.UseShellExecute = false;
27                 p.StartInfo.CreateNoWindow = true;
28                 p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//与CreateNoWindow联合使用可以隐藏进程运行的窗体
29                 p.StartInfo.RedirectStandardOutput = true;
30                 p.StartInfo.RedirectStandardInput = true;
31                 p.StartInfo.RedirectStandardError = true;
32                 p.EnableRaisingEvents = true;                      // 启用Exited事件
33                 p.Exited+=p_Exited;
34                 p.Start();
35                 if (waitTime > 0)
36                 {
37                     p.WaitForExit(waitTime);
38                 }
39 
40                 if (p.ExitCode == 0)//正常退出
41                 {
42                     //TODO记录日志
43                     System.Console.WriteLine("执行完毕!");
44                 }
45             }
46             catch (Exception ex)
47             {
48                 throw new Exception("系统错误:", ex);
49             }
50 
51         }

这里要求指定执行文件的绝对地址,当需要执行系统环境配置好的执行文件时就不好了,还要知道他的详细路径。如若这样,可以考虑下面简单的方式

1   public void RunSysExe(string filePath, string argument)
2         {
3             Process p = new Process();
4 
5             p.StartInfo.FileName = filePath;        // "iexplore.exe";   //IE
6             p.StartInfo.Arguments = argument;// "http://www.baidu.com";
7             p.Start();
8         }

最后

就写这么多了,最近鼠标抽风了,各种单机变双击......

Top
收藏
关注
评论
原文地址:https://www.cnblogs.com/Joy-et/p/4888164.html