1

I am trying to use the chkdsk.exe system tool found in C:\Windows\System32\chkdsk.exe from a C# Winforms app. I want to use the System.Diagnostics.Process Class. I am using the ProcessStartInfo to set the file name to the tool and then using the args property to set my arguments.

So I have tried using System.Diagnostics.Process with ProcessStartInfo. But when the error occurs when the user does not have the permissions the chkdsk window immediately closes. So I see the window in the task bar for a moment and then it closes without saying anything. I have tried both UseShellExecute=true and false. Also, I have tried redirecting the output stream but in a Windows app, I would have to display the output stream directly if it fails rather than a CMD window showing the error or the CHKDSK info.


 using (Process CHKDSK = new Process())
 {
     CHKDSK.StartInfo.WorkingDirectory = @"H:\";
     CHKDSK.StartInfo.FileName = "C:\\Windows\\System32\\chkdsk.exe";
     CHKDSK.StartInfo.Arguments = "/r /f C:";

     CHKDSK.StartInfo.UseShellExecute = true;
     //CHKDSK.StartInfo.UseShellExecute = false;       

     CHKDSK.Start();                
 }

I want the CHKDSK text to be displayed in a CMD window I believe. So if the user does not have permissions the window should tell the user they do not have the permissions like it would using a CMD window and typing "call chkdsk /r /f C:" I do not want to use start an instance of CMD and pass chkdsk as a parameter. I want to use just the CHKDSK.exe tool

kenlukas
  • 3,616
  • 9
  • 25
  • 36
SJCoward
  • 38
  • 7
  • Possible duplicate of [Capturing console output from a .NET application (C#)](https://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c) – Clint Jul 15 '19 at 11:54

1 Answers1

2

To do what you need to do, you must redirect standard output and also standard error streams of the process you are creating, you can redirect to console or also to anything else you want, for instance you can show the text coming from output and error streams in a multiline text box on your user interface, if you have one, or write it to a log file.

see here for some ideas on this: https://stackoverflow.com/a/285841/559144

Davide Piras
  • 43,984
  • 10
  • 98
  • 147