3

I would like to programatically run following command on command prompt, read its output and then just kill the command window.

sc query eventlog

When I run this command manually, below is what I get.

enter image description here

Here is code what I have for it.

class Program
    {
        static string output;
        static void Main(string[] args)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "sc query eventlog";
            p.OutputDataReceived += p_OutputDataReceived;
            p.Start();


            p.BeginOutputReadLine();

            p.WaitForExit();
            p.Kill();
        }

        static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            output += e.Data + Environment.NewLine;
        }

However in my case, the it just keep waiting on call for WaitForExit. For that I might have to kill this process. But I see following in the ouput varaible.

Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved.

What am I doing wrong here?

3
  • 2
    Change it so the filename is sc.exe instead of cmd.exe and remove sc from the arguments. Commented Jan 14, 2015 at 18:43
  • @MarkPM that was it. How can I answer your comment as answer? Commented Jan 14, 2015 at 18:52
  • I've added it as an answer, thanks. Commented Jan 14, 2015 at 18:55

1 Answer 1

4

You need to call sc.exe instead of cmd.exe:

p.StartInfo.FileName = "sc.exe";
p.StartInfo.Arguments = "query eventlog";
Sign up to request clarification or add additional context in comments.

Comments

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.