Call a Batch (.bat) File from ASP.NET

The following code will enable you to call a Batch File from ASP.NET / C#.

There are a few steps you must take before this will work.

1. Create a new Login ID on your machine
2. Change the site Application Pool to run under this new ID
3. Give the ID Read/Write Rights to the folder that contains the .bat file 

Code Snippet:

using System.IO; 
using System.Diagnostics;

HttpContext CTX = HttpContext.Current; 
var BatchFile = "YourBatchFile.bat";

var Path = CTX.Server.MapPath("~/YourFolderHere");
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = Path;
processInfo.FileName = BatchFile;
processInfo.UseShellExecute = true;

Process batchProcess = new Process();
batchProcess.StartInfo = processInfo; batchProcess.Start();
Comments are closed