C#.Net:为什么我的Process.Start()挂起?我正在尝试以另一个用户的身份从我的Web应用程序运行批处理文件。由于某种原因,批处理文件挂起!我可以看到“cmd.exe”在任务管理器中运行,但它永远坐在那里,不能被杀死,批处理文件不运行。这是我的代码:SecureStringpassword=newSecureString();foreach(charcin"mypassword".ToCharArray())password.AppendChar(c);ProcessStartInfopsi=newProcessStartInfo();psi.WorkingDirectory=@"c:build";psi.FileName=Environment.SystemDirectory+@"cmd.exe";psi.Arguments="/q/cbuild.cmd";psi.UseShellExecute=false;psi.UserName="建造者";psi.Password=密码;Process.Start(psi);如果您没有猜到,这个批处理文件构建了我的应用程序(与执行此命令的应用程序不同的应用程序)。Process.Start(psi);行应该立即返回,但批处理文件似乎挂起,而不是执行。有任何想法吗?编辑:有关批处理文件的内容,请参阅下面的答案。我添加了这些行:psi.RedirectStandardOutput=true;进程p=Process.Start(psi);Stringoutp=p.StandardOutput.ReadLine();并在调试模式下逐步执行它们。代码挂在ReadLine()处。我很郁闷!我相信我已经找到了答案。似乎微软以其无限的智慧阻止了WindowsServer2003中的IIS执行批处理文件。BrendenTompkins在这里有一个解决方法:http://codebetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx这对我不起作用,因为我的批处理文件使用IF和GOTO,但它肯定适用于简单的批处理文件。为什么不使用C#而不是使用批处理文件来完成所有工作?我很无聊,所以我很快就写了这篇文章,这只是我将如何做的概述,因为我不知道命令行开关的作用或文件路径。使用系统;使用System.IO;使用系统文本;使用系统安全;使用系统诊断;namespaceasdf{classStackoverflowQuestion{privateconststringMSBUILD=@"pathtomsbuild.exe";privateconststringBMAIL=@"pathtobmail.exe";privateconststringWORKING_DIR=@"pathtoworking_directory";私有字符串标准输出;私有进程p;publicvoidDoWork(){//构建项目StartProcess(MSBUILD,"myproject.csproj/t:Build",true);}publicvoidStartProcess(stringfile,stringargs,boolredirectStdout){SecureStringpassword=newSecureString();foreach(charcin"mypassword".ToCharArray())password.AppendChar(c);ProcessStartInfopsi=newProcessStartInfo();p=新进程();psi.WindowStyle=ProcessWindowStyle.Hidden;psi.WorkingDirectory=WORKING_DIR;psi.FileName=文件;psi.UseShellExecute=false;psi.RedirectStandardOutput=redirectStdout;psi.UserName="建造者";psi.Password=密码;p.StartInfo=psi;笔ableRaisingEvents=true;p.Exited+=newEventHandler(p_Exited);p.开始();如果(redirectStdout){stdout=p.StandardOutput.ReadToEnd();}}voidp_Exited(objectsender,EventArgse){if(p.ExitCode!=0){//失败的StringBuilderargs=newStringBuilder();args.Append("-sk2smtpout.secureserver.net");args.Append("-fbuild@example.com");args.Append("-tjosh@example.com");args.Append("-a"构建失败。"");args.AppendFormat("-m{0}-h",stdout);//发送邮件StartProcess(BMAIL,args.ToString(),false);}}}}如果没有看到build.cmd,很难判断发生了什么,但是,您应该使用Path.Combine(arg1,arg2)构建路径;这是构建路径的正确方法Path.Combine(Environment.SystemDirectory,"cmd.exe");我现在不记得了,但你不必设置UseShellExecute=true?“调试”它的另一种可能性是使用标准输出,然后从中读取:psi.RedirectStandardOutput=True;Processproc=Process.Start(psi);Stringwhatever=proc.StandardOutput.ReadLine();为了“看到”发生了什么,我建议你把这个过程变成更具交互性的东西(Echooff)并放一些“打印件”看看是否真的发生了什么。运行此命令后output.txt文件中的内容是什么?bmail真的执行了吗?在/之前放一些印刷品以查看发生了什么。还要在参数中添加“@”,以防万一:psi.Arguments=@"/q/cbuild.cmd";它必须非常简单:)我的猜测是build.cmd正在等待某种用户交互/回复。如果您在末尾使用“>logfile.txt”运算符记录命令的输出,它可能会帮助您找到问题。这是build.cmd的内容:@echooffsetpath=C:WINDOWSMicrosoft.NETFrameworkv2.0.50727;%path%msbuildmyproject.csproj/t:Build>output.txtIFNOTERRORLEVEL1goto:end:errorbmail-sk2smtpout.secureserver.net-fbuild@example.com-tjosh@example.com-a“构建失败。”-moutput.txt-h:enddeloutput.txt如您所见,我小心翼翼地没有输出任何东西。这一切都被发送到一个文件,如果构建失败,它会通过电子邮件发送给我。我实际上已经将这个文件作为计划任务运行了一段时间。我正在尝试构建一个允许我按需运行它的Web应用程序。感谢大家迄今为止的帮助!Path.Combine提示特别有用。我认为如果参数不正确,cmd.exe会挂起。如果批处理正确执行,那么我会像这样执行它。ProcessStartInfopsi=newProcessStartInfo();Processp=newProcess();psi.WindowStyle=ProcessWindowStyle.Hidden;psi.WorkingDirectory=@"c:build";psi.FileName=@"C:buildbuild.cmd";psi。UseShellExecute=true;psi.UserName="建造者";psi.Password=密码;p.StartInfo=psi;p.开始();也可能是cmd.exe找不到build.cmd那么为什么不给出文件的完整性路径呢?你的批次的终点是什么?如果代码挂在ReadLine上,那么问题可能是无法读取批处理文件...以上是C#学习教程:C#.Net:WhydoesmyProcess.Start()hang?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
