Magazine High tech

Exécuter un batch file (.BAT) avec paramètres à partir d'une application .NET

Publié le 21 mai 2008 par Dave Lizotte
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;
}

Retour à La Une de Logo Paperblog

A propos de l’auteur


Dave Lizotte 57 partages Voir son profil
Voir son blog

l'auteur n'a pas encore renseigné son compte l'auteur n'a pas encore renseigné son compte