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.
you'll need to use /c date to get cmd to launch date like that.
the flags /c and /k are your friends when using cmd.exe to launch other programs. /c is used to execute a program then exit CMD. /k is used to execute a program then leave CMD running.
I would also guess not all applications use stderr and stdout properly so it is possible what you see in a "Console Application" is not delivering the information where you expect it.
If nothing more redirecting stderr as well, will also allow you to see if it is syntax related and the application is tossing an exception.
I want to add to that, by wrapping it up in a class, you can interact with the cmd shell, not just run and return... If you are trying to automate an application silently, this is likely the approach you want...
I just wrote a sample of this for a guy in VB, the syntax is probably rough because I had not launched VB in years, but you get the gist of it and should be able to replicate in C# fairly easily. This was a rough draft How to type approach, not a piece of code I would call production ready, teach a man to fish sample ;)
Just to make sure there were no pitfalls, I went ahead and did this dirty in c#
So now using like this
with this on place
You have a fully interactive cmd process to write to and receive async data from.
outputs to the window
Coffee, sleep... lol
If you really want date...
Yields output
or
Yields output
By the way, in case you have not actually used the code yet, this method gives the data async, meaning as the program outputs it is delivered to you, so if you run a process that takes time, like 100 pings, traceroute, etc... you see them as they happen, not have to wait for it to complete and return.
Also you can pass commands back to the application during that, like canceling, reacting to and changing syntax, or simply running something else similar based on the result of the first run.
essentially you can treat it just like you were typing in a cmd window, and receiving the feedback there, wrap the whole think in a properly threaded form (Careful when updating windows controls from other threads), and you would have an emulated cmd prompt.