Running chkdsk from my code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to run check disk on the c: (ntfs) on my computer via some code.
I can launch the shell and give it the command to do the disk checking but it
requires that I enter a "y" after some dialog in the cmd prompt comes up.
Does anyone have a way to make this work?

Thanks
 
because when you do that, it doesn't run the check disk. Do you think I
would have to mark the disk as "dirty"?
 
System.Diagnostics.Process.Start("chkdsk.exe", "C:").WaitForExit();

worked for me just fine....how did it "not work"?
 
because when I use the /f opition it still prompts for a yes or no answer.

Untested Console Project - let me know how it works:

Imports System.Diagnostics

Module Module1

Sub Main()
Dim info As New ProcessStartInfo()
info.FileName = "cmd.exe"
info.UseShellExecute = False
info.RedirectStandardInput = True
Using p As New Process()
p.StartInfo = info
p.Start()
p.StandardInput.WriteLine("chkdsk c: /f")
p.StandardInput.WriteLine("y")
p.Close()
End Using
End Sub

End Module

Thanks,

Seth Rowe
 
Dim proc As New Process
proc = Process.Start("chkdsk.exe", "C /f")
Thread.Sleep(2000) ' ----Give it some time
SendKeys.Send("N~") '--Send N and Enter
 
That progie below didn't work either. Any other thoughts out there?










- Show quoted text -

What went wrong?

I'm guessing it might be how quickly the "y" is written, try adding
System.Threading.Thread.Sleep(1000) before you send the "y" to the
command prompt.

Thanks,

Seth Rowe
 
Use this if you want to suppress the command window:

Dim WithEvents proc As New Process

Private Sub RunChkdsk()
proc.StartInfo.FileName = "chkdsk.exe"
proc.StartInfo.Arguments = "C /f"
proc.StartInfo.UseShellExecute = False
proc.StartInfo.RedirectStandardInput = True
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.CreateNoWindow = True
proc.EnableRaisingEvents = True
proc.Start()
proc.StandardInput.WriteLine("Y")
End Sub

Private Sub proc_Exited(ByVal sender As Object, ByVal e As System.EventArgs)
Handles proc.Exited
Dim fs As StreamReader = proc.StandardOutput
Debug.WriteLine(fs.ReadToEnd)
fs.Close()
proc.Close()
End Sub
 
I found 2 answers to this the first is :

Shell("cmd /c echo Y | chkdsk c: /f/r", AppWinStyle.NormalFocus, True)

the /c is what makes this work and

Shell("fsutil dirty set c:", AppWinStyle.NormalFocus, True)
Sleep(100)
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CURRENTCONTROLSET\CONTROL\Session Manager", "BootExecute", "autocheck autochk /r \??\C:")

Thanks for the help on this though.
 
Back
Top