Tuesday, November 21, 2006

Invoking Executeable File from ASP.NET Web Services

Below is the code snippet on how to invoke the executable file (exe) from the cmd.exew being executed.

// Execute the Command Prompt
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");

// Indicating the input read from the process's standard input
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;

// Do not show the GUI of the running app
psi.CreateNoWindow = true;

Process process = null;
try
{
// Execute the cmd.exe
process = Process.Start(psi);

StreamWriter writer = process.StandardInput;

// Write the command to the command prompt
writer.WriteLine("notepad");

}
catch (Exception ex)
{
// do something here
}
finally
{
if (process != null)
{
process.Close();
}
}


The code will simply run the notepad from the cmd.exe. You can just call "notepad" from at line the of ProcessStartInfo psi = new ProcessStartInfo("notepad");, but I am showing how are you going to write the command to the cmd using the StandardInput of Process object.

Similarly, you can call batch file from the cmd to execute command in the batch file too, or store those static command in the resource files (resx) inside the app_GlobalResources folder and get from the key by

Resources.<Resource file name>.<key name>

No comments: