Run batch file in C #

I have a batch file that basically downloads a file from the site or returns an error message saying that the files are not available. I want to execute this .bat file in a C # component (SSIS script) and save the file name in a variable (if the file is loaded) or assign an error message to a variable.

Thank you in advance

+3
source share
2 answers

You can use System.Diagnostics.Process to run Cmd.exe with the / C switch. Specify the name of the batch file and it is mainly there. To simplify the process of creating and capturing std input and output, I recommend looking at the ProcessRunner class , which I put together a while ago. If you do not read this article in How to properly use System.Diagnostics.Process .

Alternatively, you can look at the ScriptRunner class in the same library that creates the shell for running several different types of Windows script scripts.

0
source

Process.Start. , StandardError StandardOutput Process. RedirectStandardOutput true.

MSDN:

 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "yourbatchfile.bat";
 p.Start();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();
+10

Source: https://habr.com/ru/post/1784573/


All Articles