views:

131

answers:

2

I want to call from a c# application a command line starting from it an application and retrieve the stat from it.

I did this but something is missing:

ProcessStartInfo psf = new ProcessStartInfo("cmd.exe", "/C time");
psf.WindowStyle = ProcessWindowStyle.Hidden;
psf.RedirectStandardOutput = true;
psf.UseShellExecute = false;
psf.CreateNoWindow = true;
Process p = Process.Start(psf);
StreamReader sr = p.StandardOutput;
p.WaitForExit();

What is wrong ?

+2  A: 

Try passing "/c time /t" instead of "/c time".

Lukas Pokorny
Yes i got the data. What did /t did ? How can I get also the miliseconds ?
Roman Dorevich
As others stated above, without /t, the command asks for input. But I don't think the "time" command supports miliseconds. This has also been stated before, but why not simply use DateTime.Now instead?
Lukas Pokorny
I need to check from differences between this option and the DateTime object and another thing. Thanks
Roman Dorevich
So what about using "echo %time%" instead of "/c time /t"? Can you use that instead?
Lukas Pokorny
Create a process and to the command line to send this arguments ?
Roman Dorevich
Yes, try new ProcessStartInfo("cmd.exe", "/C echo %time%") - this produces output like "10:32:41,18", which is nearly what you need.
Lukas Pokorny
+1  A: 

To get the system time I would recommend you using the DateTime structure:

string time = DateTime.Now.ToString("hh:mm:ss.fff");
Console.WriteLine(time);
Darin Dimitrov
I need to use shell and time and not the datewtime object
Roman Dorevich