0

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 ?

4
  • cmd/c Time expects user input. It won't close unless you provide some input to it. In your case, you are hiding the command window. That makes it look like an input is pending in hidden window. what do you want the code to do? Commented Feb 21, 2010 at 8:56
  • I don't want to change the time only getting it from this application Commented Feb 21, 2010 at 8:57
  • 1
    Could you not use DateTime.Now? Commented Feb 21, 2010 at 8:58
  • 1
    In that case, use cmd /c time /t as @Lukas has said. Commented Feb 21, 2010 at 9:02

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

Yes i got the data. What did /t did ? How can I get also the miliseconds ?
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?
I need to check from differences between this option and the DateTime object and another thing. Thanks
So what about using "echo %time%" instead of "/c time /t"? Can you use that instead?
Yes, try new ProcessStartInfo("cmd.exe", "/C echo %time%") - this produces output like "10:32:41,18", which is nearly what you need.
|
1

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);

1 Comment

I need to use shell and time and not the datewtime object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.