Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a batch file from an ASP.Net page

Tags:

asp.net

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.

Could anybody please tell me what I'm doing wrong?

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

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

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.

UPDATE

The following code works fine (it runs the batch file):

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

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

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();
like image 268
Jimmy Collins Avatar asked May 04 '11 14:05

Jimmy Collins


People also ask

How do I run a batch file from a website?

Just create a batch file and save in the location where your html file is there. this anchor tag will execute the (test. bat) batch file. After clicking on the link <TEST>, you will get the window prompting to open/save/close, if you will click on open then batch file will be executed.

How do I run a batch file from an argument in C#?

string MyBatchFile = @"C:\Program Files (x86)\MybatchFile. bat"; string _sourcePath = @"C:\FolderToCopy"; string _tempTargetPath = @"C:\TargetFolder\"; var process = new Process { StartInfo = { Arguments = string. Format("{0} {1}", _sourcePath, _tempTargetPath) } }; process.

What is batch file in asp net?

It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF , FOR , and GOTO labels.

How do I run a batch file in Visual Studio setup project?

In the "File System" view (right-click on the project in Solution Explorer->View->File System), add the batch file you want to execute and cmd.exe (C:\Windows\System32\cmd.exe)


2 Answers

Just call the batch file directly:

p.StartInfo.FileName = batchFileLocation;

Also, make sure the WorkingDirectory is set to the right location:

p.StartInfo.WorkingDirectory= Path.GetDirectoryName(batchFileLocation);
like image 133
Oded Avatar answered Oct 03 '22 03:10

Oded


A little google on "Faulting application cmd.exe" points me to this IIS forum.

It seems that you cannot create a new process in the background under IIS, unless you use the CreateProcessWithLogon method. (I have not tested this).

like image 22
GvS Avatar answered Oct 03 '22 03:10

GvS