Running a batch file from an ASP.Net page
<p>I'm trying to run a batch file on a server via an ASP.Net page, and it's driving my crazy. When I run the below code, nothing happnes - I can see from some log statements that this code runs, but the .bat file that I pass to the function never runs.</p>

<p>Could anybody please tell me what I'm doing wrong?</p>

<pre><code>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();
}
</code></pre>

<p>In the 'Application' Event Viewer log on the server, every time I try to run this, the following issue seems to occur:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p><strong>UPDATE</strong></p>

<p>The following code works fine (it runs the batch file):</p>

<pre><code>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();
</code></pre>

<p>This however doesn't (when i try to run as a specific user):</p>

<pre><code>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();
</code></pre>
