【发布时间】:2012-11-24 17:57:10
【问题描述】:
我试图通过 ASP.Net 页面在服务器上运行批处理文件,这让我发疯。当我运行下面的代码时,什么都没有发生 - 我可以从一些日志语句中看到该代码运行,但我传递给函数的 .bat 文件从未运行。
谁能告诉我我做错了什么?
public void ExecuteCommand(string batchFileLocation)
{
Process p = new Process();
// Create secure password
string prePassword = "myadminpwd";
SecureString passwordSecure = new SecureString();
char[] passwordChars = prePassword.ToCharArray();
foreach (char c in passwordChars)
{
passwordSecure.AppendChar(c);
}
// Set up the parameters to the process
p.StartInfo.FileName = @"C:\\Windows\\System32\cmd.exe";
p.StartInfo.Arguments = @" /C " + batchFileLocation;
p.StartInfo.LoadUserProfile = true;
p.StartInfo.UserName = "admin";
p.StartInfo.Password = passwordSecure;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
// Run the process and wait for it to complete
p.Start();
p.WaitForExit();
}
在服务器上的“应用程序”事件查看器日志中,每次我尝试运行它时,似乎都会出现以下问题:
错误应用程序 cmd.exe,版本 6.0.6001.18000,时间戳 0x47918bde,错误模块 kernel32.dll,版本 6.0.6001.18000,时间戳 0x4791a7a6,异常代码 0xc0000142,错误偏移量 0x00009cac,进程 id 0x47bc,应用程序启动时间 0xa678d5408
更新
以下代码运行良好(它运行批处理文件):
Process p = new Process();
p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = false;
// Run the process and wait for it to complete
p.Start();
p.WaitForExit();
但这不会(当我尝试以特定用户身份运行时):
Process p = new Process();
// Create secure password
string prePassword = "adminpassword";
SecureString passwordSecure = new SecureString();
char[] passwordChars = prePassword.ToCharArray();
foreach (char c in passwordChars)
{
passwordSecure.AppendChar(c);
}
p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = false;
p.StartInfo.UserName = "admin";
p.StartInfo.Password = passwordSecure;
// Run the process and wait for it to complete
p.Start();
p.WaitForExit();
【问题讨论】:
标签: asp.net