Pages

Tuesday, December 8, 2009

Executing a batch file with parameter from C#

hello All,
Some time we need to run or execute a batch file inside our code behind using the c# code, for that reason here I am going to show that how can we run a batch file inside the code behind. We can also pass the required parameter to run the file. Please check the following code.


try
{
System.Diagnostics.Process() objProcess = new System.Diagnostics.Process(); // Create the process object
objProcess.
StartInfo.FileName = "c:\\Anil\\testAnil; //batch File Name
objProcess.StartInfo.Arguments = "
c:\\Anil\\testAnil" + strXmlFileName;//paramter file name
objProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
objProcess.Start();
//Wait until the process passes back an exit code
objProcess.WaitForExit();
//Free resources associated with this process
objProcess.Close();
}
catch (Exception ex)
{
MessageBox.Show("
Could not start process " + FileNameNew, "Error");
}


I hope this will solve your problem. Waiting for your comments..!! Happy Coding!!


Thanks
Anil Kumar Pandey
System Architect, MVP
Mumbai, Maharshtra

No comments:

Post a Comment

Kontera