加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 03:54:42 所属栏目:asp.Net 来源:网络整理
导读:我正试图通过ASP.Net页面在服务器上运行批处理文件,这让我发疯了.当我运行下面的代码时,没什么好开心的 – 我可以从一些日志语句中看到这段代码运行,但我传递给函数的.bat文件永远不会运行. 有谁能告诉我我做错了什么? public void ExecuteCommand(string b
我正试图通过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:WindowsSystem32cmd.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,time stamp 0x4791a7a6,exception code 0xc0000142,fault offset 0x00009cac,process id 0x8bc,application start time 0x01cc0a67825eda4b.

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();

解决方法

一个关于“Faulting应用程序cmd.exe”的小谷歌指向我 IIS forum.

除非您使用CreateProcessWithLogon方法,否则您似乎无法在IIS下的后台创建新进程. (我没有测试过这个).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读