Execute batch file
A function to executing the batch file from C#
The value of the first argument [batchFileName] is full path to batch file.
The value of the second argument [argumentsToBatchFile] is array of argument if exist, sending to batch file.
protected bool ExecuteBatchFile(string batchFileName, string[] argumentsToBatchFile)
{
string argumentsString = string.Empty;
try
{
if (argumentsToBatchFile != null)
{
for (int count = 0; count < argumentsToBatchFile.Length; count++)
{
argumentsString += " ";
argumentsString += argumentsToBatchFile[count];
}
}
System.Diagnostics.ProcessStartInfo DBProcessStartInfo = new System.Diagnostics.ProcessStartInfo(batchFileName, argumentsString);
DBProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
DBProcessStartInfo.UseShellExecute = true;
System.Diagnostics.Process dbProcess;
dbProcess = System.Diagnostics.Process.Start(DBProcessStartInfo);
while (!dbProcess.HasExited)
dbProcess.WaitForExit(2000);
return true;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
Unique visitors to post: 2