本文介绍了C#.net:为什么我的Process.Start()挂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个批处理文件,作为另一个用户,从我的web应用程序。出于某种原因,该批处理文件挂起!我可以看到cmd.exe的任务管理器中运行,但它只是坐在那里永远,无法被杀,批处理文件没有运行。这是我的code:

I'm trying to run a batch file, as another user, from my web app. For some reason, the batch file hangs! I can see "cmd.exe" running in the task manager, but it just sits there forever, unable to be killed, and the batch file is not running. Here's my code:

SecureString password = new SecureString();
foreach (char c in "mypassword".ToCharArray())
    password.AppendChar(c);

ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = @"c:\build";
psi.FileName = Environment.SystemDirectory + @"\cmd.exe";
psi.Arguments = "/q /c build.cmd";
psi.UseShellExecute = false;
psi.UserName = "builder";
psi.Password = password;
Process.Start(psi);

如果你没猜错,这个批处理文件建立我的应用程序(不同的应用程序比正在执行此命令的)。

If you didn't guess, this batch file builds my application (a different application than the one that is executing this command).

本的Process.Start(PSI);行立即返回,因为它应该,但该批处理文件只是似乎挂,不执行。任何想法?

The Process.Start(psi); line returns immediately, as it should, but the batch file just seems to hang, without executing. Any ideas?

编辑:查看下面的批处理文件的内容,我的答案

See my answer below for the contents of the batch file.

  • 在此output.txt从来没有被创建。

我添加了这些行:

psi.RedirectStandardOutput = true;
Process p = Process.Start(psi);
String outp = p.StandardOutput.ReadLine();

,并通过他们在调试模式下阶梯。在code挂在的ReadLine()。我难倒!

推荐答案

我相信我已经找到了答案。看来微软在其所有无穷的智慧,已经从由IIS在Windows Server 2003布兰登·汤普金斯正在执行封锁批处理文件有一个工作在这里:

I believe I've found the answer. It seems that Microsoft, in all their infinite wisdom, has blocked batch files from being executed by IIS in Windows Server 2003. Brenden Tompkins has a work-around here:

<一个href="http://$c$cbetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx">http://$c$cbetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx

对我来说这是行不通的,因为我的批处理文件使用IF和GOTO,但它肯定会进行简单的批处理文件的工作。

That won't work for me, because my batch file uses IF and GOTO, but it would definitely work for simple batch files.

这篇关于C#.net:为什么我的Process.Start()挂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:34