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

1、Process基本属性和方法

Id          //进程的Id

ProcessName      //进程的名称

PriorityClass      //进程的优先级

HandleCount      //进程句柄数

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

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

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

.

.

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

Start()     //启动进程

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

.

.

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

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

  /// <summary>
/// 执行Cmd
/// </summary>
/// <param name="argument">cmd命令</param>
/// <param name="msg">返回信息</param>
/// <param name="directoryPath">路径</param>
/// <param name="closed">是否关闭</param>
public static void RunCmd(string argument,out string msg, string directoryPath = "",bool redirect=false)
{
msg = string.Empty;
Process process = new Process();
ProcessStartInfo startInfo=new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments=redirect?@"/c "+argument:@"/k "+argument;
startInfo.UseShellExecute=false; //是否需要启动windows shell
startInfo.CreateNoWindow=false;
startInfo.RedirectStandardError=redirect; //是否重定向错误
startInfo.RedirectStandardInput = redirect; //是否重定向输入 是则不能在cmd命令行中输入
startInfo.RedirectStandardOutput = redirect; //是否重定向输出,是则不会在cmd命令行中输出
startInfo.WorkingDirectory=directoryPath; //指定当前命令所在文件位置,
process.StartInfo = startInfo;
process.Start();
if (redirect)
{
process.StandardInput.Close();
msg = process.StandardOutput.ReadToEnd(); //在重定向输出时才能获取
}
//else
//{
// process.WaitForExit();//等待进程退出
//}
}

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

3、启动exe

  /// <summary>
/// 启动exe
/// </summary>
/// <param name="filePath">程序路径</param>
/// <param name="argument">参数</param>
/// <param name="waitTime">等待时间,毫秒计</param>
public void RunExe(string filePath,string argument,int waitTime=0)
{
if (string.IsNullOrEmpty(filePath))
{
throw new Exception("filePath is empty");
}
if (!File.Exists(filePath))
{
throw new Exception(filePath + " is not exist");
}
string directory = Path.GetDirectoryName(filePath); try
{
Process p = new Process();
p.StartInfo.FileName = filePath;
p.StartInfo.WorkingDirectory = directory;
p.StartInfo.Arguments = argument;
p.StartInfo.ErrorDialog = false;
//p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//与CreateNoWindow联合使用可以隐藏进程运行的窗体
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.EnableRaisingEvents = true; // 启用Exited事件
p.Exited+=p_Exited;
p.Start();
if (waitTime > )
{
p.WaitForExit(waitTime);
} if (p.ExitCode == )//正常退出
{
//TODO记录日志
System.Console.WriteLine("执行完毕!");
}
}
catch (Exception ex)
{
throw new Exception("系统错误:", ex);
} }

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

   public void RunSysExe(string filePath, string argument)
{
Process p = new Process(); p.StartInfo.FileName = filePath; // "iexplore.exe"; //IE
p.StartInfo.Arguments = argument;// "http://www.baidu.com";
p.Start();
}

最后

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

C#启动进程之Process的更多相关文章

  1. 进程之 Process join方法其他属性与进程Queue

    Process join方法 以及其他属性 在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情况 情况一:在主进程的任务与子进程的任务彼此独立的情 ...

  2. python多进程之Process

    由于fork创建进程不能在windows系统上使用,所以产生了multiprocessing.Process Process可以直接实例化然后用start调用,需要指定新的进程执行的函数,用元组的方式 ...

  3. 创建多进程之multiprocess包中的process模块

    创建多进程之multiprocess包中的process模块 1.process模块是一个创建进程的模块 Process([group [, target [, name [, args [, kwa ...

  4. 多进程之multiprocessing模块和进程池的实现

    转载:https://www.cnblogs.com/xiaobeibei26/p/6484849.html Python多进程之multiprocessing模块和进程池的实现 1.利用multip ...

  5. 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6621566 上一篇文章Android进程间通信 ...

  6. 缺陷的背后(四)---多进程之for循环下fork子进程引发bug

    导语 业务模块为实现高并发时的更快的处理速度,经常会采用多进程的方式去处理业务.多进程模式下常见的三种bug:for循环下fork子进程导致产生无数孙子进程,僵尸进程,接口窜包.本章主要介绍第一种常见 ...

  7. 多进程之multiprocessing模块、守护进程、互斥锁

    目录 1. multiprocessing 模块介绍 2. Process类的介绍 3. Process类的使用 4. 守护进程 5. 进程同步(锁) 1. multiprocessing 模块介绍 ...

  8. storm启动过程之源码分析

    TopologyMaster: 处理拓扑的一些基本信息和工作,比如更新心跳信息,拓扑指标信息更新等   NimbusServer: ** * * NimbusServer work flow: 1. ...

  9. linux进程之fork 和 exec函数

    ---恢复内容开始--- fork函数 该函数是unix中派生新进程的唯一方法. #include <unistd.h> pid_t   fork(void); 返回: (调用它一次, 它 ...

随机推荐

  1. SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge (并不在同一个版本库中)

    在使用svn merge命令报错 英文版本:SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge 中文版本报错:并不 ...

  2. 用链表实现栈----《数据结构与算法分析----C语言描述》

    一.头文件: #ifndef _STACK_LINK_H_ #define _STACK_LINK_H_ struct stack_record; typedef struct stack_recor ...

  3. HttpClient请求服务器代码优化版

    HttpClient请求服务器代码优化版 首先,我在前面的两篇博文中介绍了在 Android中,除了使用java.net包下HttpUrlConnection的API访问HTTP服务之外,我们还可以换 ...

  4. hdu5115(区间dp)

    n头狼排成一列,每头狼有两个属性,基础攻击力和附加攻击力, 第i只狼的基础攻击力是ai,附加攻击力是b(i-1) + b(i+1) 消灭一只狼,受到的伤害为基础攻击力+附加攻击力. 问消灭所有的狼受到 ...

  5. lightoj1027(期望dp)

    有一个迷宫,有n个门,走每个的概率都是相同的 每个门有一个数字,如果是正数ai,那么表示走ai天就能走出迷宫,如果是负数,那么走-ai天会回到原地,然后会忘记之前的事情,继续选择门去走 所以,如果都是 ...

  6. NSUserDefaults写作和阅读对象定义自己

    需要编写对象必须实现NSCoding protocol Person Class Person.h #import <Foundation/Foundation.h> #import &q ...

  7. Qt 3D研究(九):尝试第二边缘检测方法

    Qt 3D研究(九):尝试第二边缘检测方法 三维应用程序,通过FBO.将3D图像渲染成纹理,然后对渲染成的纹理进行图像处理,终于显示在屏幕上的.是风格化后的图案.上一次我使用了一种普通的图像处理方法: ...

  8. 《Effective C++ 》学习笔记——规定10

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  9. 【ThinkingInC++】61、非成员运算符

    非成员运算符 当操作者的左侧是不同的类时.运算符重载不可能是正确的类中. IostreamOperatorOverloading.cpp /** * 书本:[ThinkingInC++] * 功能:非 ...

  10. MyEclipse建立JVM内存大小

    1.你为什么要设置JVM它的内存大小? 因为假定程序执行过JVM默认的内存大小.他们不会报告传说中的内存溢出异常. Exception in thread "main" java. ...