none
Execute chkdsk and display its output in C# RRS feed

  • Question

  • Hi,

    I want to execute chkdsk from my application and display it's result. Here is my code. When it runs the chkdsk finishes very quickly and i do not get anything from the standard output. If i am executing it with ProcessWindowStyle.Normal a dialog appears for 1-2 seconds with the execution result in it and than it closes.

    Any idea why this code cannot read from standard output?

    I have tried also a delegate attaching it to process.StartInfo.RedirectStandardOutput += ... but it did not work too.

    I would very appriciated for any help!


           public void DoWork(object sender, DoWorkEventArgs e)
            {
                try
                {
                    // set process parameters
                    System.Diagnostics.Process process = new System.Diagnostics.Process();

                    process.StartInfo.CreateNoWindow = false;
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    process.StartInfo.FileName = "ChkDsk.exe";
                    process.StartInfo.Arguments = string.Concat("e:", " /f");
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardOutput = true; 

                    process.Start();

                    while (!process.HasExited)
                    {
                        if ((sender as BackgroundWorker).CancellationPending )
                        {
                            process.Kill();
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    e.Result = process.StandardOutput.ReadToEnd();
                    process.WaitForExit();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
      
            }

    Regards,

    f.

    Monday, February 18, 2013 10:37 PM

Answers

All replies

  • Here a two examples on how to run Controll a Console programm from a GUI:

    http://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

    http://stackoverflow.com/questions/6760341/control-console-application-from-windows-form-application-c-sharp

    Don't forget that rights might pose a problem. By default your programm (and everythign it starts) will propably run under limited user rights (asuming Vista+ with UAC enabeled).

    And in most cases chkdsk can't work with anything less than full admin rights.

    If you want your application to always run in Admin mode you have to modify the manifest (so the .NET Runtime knows of this requriement and can request a run at the right rights):

    http://www.developerfusion.com/code/7987/making-a-net-app-run-on-vista-with-administrator-priviledges/


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Proposed as answer by CoolDadTxModerator Tuesday, February 19, 2013 2:49 PM
    • Marked as answer by flodoardo Wednesday, February 20, 2013 4:55 PM
    Tuesday, February 19, 2013 12:25 AM
  • I'd lean towards a permissions issue with not being an admin as to why it runs so fast.  As to why you don't see the output it is because you aren't handling the stream properly I believe.  The MSDN documentation here provides a very good example of how to do it correctly.  Also note that your code runs the risk of not working if there are lots of errors on the drive because the buffer will fill up.  Once the buffer is full the underlying process will block until you empty it (which won't happen because you are waiting for the process to end).

    Michael Taylor - 2/19/2013
    http://msmvps.com/blogs/p3net

    Tuesday, February 19, 2013 2:59 PM
    Moderator
  • Try the CSConsole program on my SkyDrive.  Do you get the same results?

    Tuesday, February 19, 2013 7:10 PM
  • Hi,

    Thanks for all of you! the solution was to add a new line to the code:

    process.StartInfo.RedirectStandardInput = true;

    this will give the result i need.

    So here is the final solution:

        // set process parameters
        System.Diagnostics.Process process = new System.Diagnostics.Process();

        process.StartInfo.CreateNoWindow = false;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.StartInfo.FileName = "ChkDsk.exe";
        process.StartInfo.Arguments = string.Concat("e:", " /f");
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardInput = true;

        process.Start();

        while (!process.HasExited)
        {
            if ((sender as BackgroundWorker).CancellationPending )
            {
                process.Kill();
                break;
            }
            Thread.Sleep(100);
        }
        e.Result = process.StandardOutput.ReadToEnd();
        process.WaitForExit();

    Let me mark as the first reply as answer because the solution is there (i also read it before but did not think that this could be the root cause...)

    @JohnWein: thanks for your example too, it was also very-very usefull!

    Regards,

    flodoardo

    Wednesday, February 20, 2013 4:55 PM