我试图通过 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 0x8bc,应用程序启动时间0x01 cc0a67825eda4b。
UPDATE
以下代码工作正常(它运行批处理文件):
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();
直接调用批处理文件即可:
p.StartInfo.FileName = batchFileLocation;
另外,请确保WorkingDirectory设置到正确的位置:
p.StartInfo.WorkingDirectory= Path.GetDirectoryName(batchFileLocation);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)