I want to write a c# code to execute some command prompt lines and store the result into a global variable in c# to be used for further processing in other part of the code in c#.
I bought a device and I installed its driver. The only way for me to obtain its data is using command line in command prompt. There is no API available. But, I want to do in c# so that is why I am having trouble.
I have some code and it doesn't works for some reason. Note, I used argument input = "date" is only for illustration purpose. My work is not using "date" but some arguments to obtain data.
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// the cmd program
startInfo.FileName = "cmd.exe";
// set my arguments. date is just a dummy example. the real work isn't use date.
startInfo.Arguments = "date";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
// capture what is generated in command prompt
var output = process.StandardOutput.ReadToEnd();
// write output to console
Console.WriteLine(output);
process.WaitForExit();
Console.Read();
}
Any help is appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…