简体   繁体   中英

handling an executable file in Visual Studio C#

My env is Visual Studio 2008 C# and .Net. I like to call an executable from the code and pass parameters to it and then get the output. How do I do this?

Thanks Bruce

Use Process.Start to start a process. How you give it data and get the output will depend on the executable, but you can pass command line arguments, and if it writes its output to a file, that will be simple. You can capture its standard output, standard error and standard input streams too - ideally use ProcessStartInfo to configure everything you need before starting the process.

Reading/writng data from the process via the standard output/error/input can be slightly tricky (as you may need to use multiple threads) so if you can specify command line arguments to specify input and output files, that would be ideal.

processInfo.FileName =FileName.exe;
processInfo.WorkingDirectory = fileDirectory;
processInfo.RedirectStandardInput =false;
processInfo.CreateNoWindow =true;
processInfo.UseShellExecute =false;
Process startRestore = Process.Start(processInfo);
startRestore.WaitForExit();

Replace your file with "FileName.exe" and File path with fileDirectory. I hope it will be helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM