Aujourd'hui nous allons voir comment appeler à partir d'une application .NET, un batch file (.bat) et ce en lui passant des paramètres. Voici donc comment faire cela grâce à VB.NET.
public string executeBAT(string path, string batchFile, string batchParameters)
{
//path "c:\\chemin_d_acces\\"
//batchFile "monbatchfile.bat"
//batchParameters "p1 p2 p3"
string processedResult = "";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = path;
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader sOut = proc.StandardOutput;
System.IO.StreamWriter sIn = proc.StandardInput;
sIn.WriteLine(batchFile + " " + batchParameters);
sIn.WriteLine("EXIT");
proc.Close();
string results = sOut.ReadToEnd().Trim();
results = results.Replace(System.Environment.NewLine, "<br>");
sIn.Close();
sOut.Close();
return processedResult;
}