简体   繁体   中英

Give user input for batch file via c# code

I have a batch file which is asking for some user input text in the middle of that process. I need to run this batch file through c# code. I cannot change the bat file. So the option having command line arguments also is not working for me. Is there any way that i can provide the input when it prompts?

I assume you are using Process.Start to run the batch script. In this case you can easily write to the script's standard input, as if you were typing directly on the commandline:

Process process = new Process();
process.StartInfo.FileName = "script.bat";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("whatever the parameter is...");

If necessary, you can wait for the script to ask for the input by setting:

process.StartInfo.RedirectStandardOutput = true;

and "listening" to its standard output using the process.StandardOutput.Read(); and similar methods

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