gpt4 book ai didi

asp.net - 从 ASP.Net 页面运行批处理文件

转载 作者:行者123 更新时间:2023-12-04 15:15:16 25 4
gpt4 key购买 nike

我正在尝试通过 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();
}

在服务器上的“应用程序”事件查看器日志中,每次我尝试运行它时,似乎都会出现以下问题:

Faulting application cmd.exe, version 6.0.6001.18000, time stamp 0x47918bde, faulting module kernel32.dll, version 6.0.6001.18000, time stamp 0x4791a7a6, exception code 0xc0000142, fault offset 0x00009cac, process id 0x8bc,application start time 0x01cc0a67825eda4b.



更新

以下代码工作正常(它运行批处理文件):
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);

关于asp.net - 从 ASP.Net 页面运行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5884939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com