本篇文章给大家分享的是有关C#中怎么调用外部进程,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
执行外部进程的函数
privatestringRunCmd(stringcommand) { //例Process Processp=newProcess(); p.StartInfo.FileName="cmd.exe";//确定程序名 p.StartInfo.Arguments="/c"+command;//确定程式命令行 p.StartInfo.UseShellExecute=false;//Shell的使用 p.StartInfo.RedirectStandardInput=true;//重定向输入 p.StartInfo.RedirectStandardOutput=true;//重定向输出 p.StartInfo.RedirectStandardError=true;//重定向输出错误 p.StartInfo.CreateNoWindow=true;//设置置不显示示窗口 p.Start();//00 //p.StandardInput.WriteLine(command);//也可以用这种方式输入入要行的命令 //p.StandardInput.WriteLine("exit");//要得加上Exit要不然下一行程式 returnp.StandardOutput.ReadToEnd();//输出出流取得命令行结果果 }
这个方法应该是比较常见的C#调用外部进程的方法,我以前也一直是这样调用外部进程的,也没有碰到过什么问题。但这次调用的外部进程比较特殊,用这种方法调用就出现了两个问题。
***个问题是这个被调用的外部进程有时候会出现异常,出现异常后Windows会弹出错误报告框,程序于是吊死在那里,必须手工干预。这个问题比较好解决,程序中设置一下注册表搞定。
第二个问题是C#调用外部进程(是一个控制台进程)后,程序会阻塞在p.StandardOutput.ReadToEnd();这一句,永远无法出来,被调用的那个控制台程序也被吊死。但该控制台进程在CMD 中是可以正常执行的。后来看来一些资料才发现原来原因是出在该控制台程序控制台输出大量字符串,管道重定向后,调用程序没有及时将管道中的输出数据取出,结果导致管道被阻塞,程序吊死。在这里还有另外一个问题,虽然这次没有遇到,但网上有其他人遇到,就是错误信息管道不及时取出数据,也会被阻塞,而且如果要同时取出两个管道的数据,必须要利用一个辅助线程才能实现。
问题讲完了,下面给出这个类的完整代码
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Runtime.InteropServices; usingSystem.Threading; namespaceLaboratory.Process { classReadErrorThread { System.Threading.Threadm_Thread; System.Diagnostics.Processm_Process; Stringm_Error; boolm_HasExisted; objectm_LockObj=newobject(); publicStringError { get { returnm_Error; } } publicboolHasExisted { get { lock(m_LockObj) { returnm_HasExisted; } } set { lock(m_LockObj) { m_HasExisted=value; } } } privatevoidReadError() { StringBuilderstrError=newStringBuilder(); while(!m_Process.HasExited) { strError.Append(m_Process.StandardError.ReadLine()); } strError.Append(m_Process.StandardError.ReadToEnd()); m_Error=strError.ToString(); HasExisted=true; } publicReadErrorThread(System.Diagnostics.Processp) { HasExisted=false; m_Error=""; m_Process=p; m_Thread=newThread(newThreadStart(ReadError)); m_Thread.Start(); } } classRunProcess { privateStringm_Error; privateStringm_Output; publicStringError { get { returnm_Error; } } publicStringOutput { get { returnm_Output; } } publicboolHasError { get { returnm_Error!=""&&m_Error!=null; } } publicvoidRun(StringfileName,Stringpara) { StringBuilderoutputStr=newStringBuilder(); try { //disabletheerrorreportdialog. //reference:http://www.devcow.com/blogs/adnrg/archive/2006/07/14/Disable-Error-Reporting-Dialog-for-your-application-with-the-registry.aspx Microsoft.Win32.RegistryKeykey; key=Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"software\microsoft\PCHealth\ErrorReporting\",true); intdoReport=(int)key.GetValue("DoReport"); if(doReport!=0) { key.SetValue("DoReport",0); } intshowUI=(int)key.GetValue("ShowUI"); if(showUI!=0) { key.SetValue("ShowUI",0); } } catch { } m_Error=""; m_Output=""; try { System.Diagnostics.Processp=newSystem.Diagnostics.Process(); p.StartInfo.FileName=fileName; p.StartInfo.Arguments=para; p.StartInfo.UseShellExecute=false; p.StartInfo.RedirectStandardInput=true; p.StartInfo.RedirectStandardOutput=true; p.StartInfo.RedirectStandardError=true; p.StartInfo.CreateNoWindow=true; p.Start(); ReadErrorThreadreadErrorThread=newReadErrorThread(p); while(!p.HasExited) { outputStr.Append(p.StandardOutput.ReadLine()+"\r\n"); } outputStr.Append(p.StandardOutput.ReadToEnd()); while(!readErrorThread.HasExisted) { Thread.Sleep(1); } m_Error=readErrorThread.Error; m_Output=outputStr.ToString(); } catch(Exceptione) { m_Error=e.Message; } } } }
以上就是C#中怎么调用外部进程,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。