Hi!
At some point in my application, I do a "Analyze disk" using the defrag command with attribute "-a -v". The application works fine in Win XP, but in Win 7, the command don't seems to be executed at all!
Doing some research, I found that I need to add "StartInfo.Verb = "runas" " to run my defrag as administrator. But this don't seems to be enough because I still get a NullReferenceException(clsProcess.StandardOutput seems to return nothing). Maybe someone can help me on this...
Thanks in advance!
At some point in my application, I do a "Analyze disk" using the defrag command with attribute "-a -v". The application works fine in Win XP, but in Win 7, the command don't seems to be executed at all!
Doing some research, I found that I need to add "StartInfo.Verb = "runas" " to run my defrag as administrator. But this don't seems to be enough because I still get a NullReferenceException(clsProcess.StandardOutput seems to return nothing). Maybe someone can help me on this...
VB.NET:
Dim clsProcess As New System.Diagnostics.Process()
clsProcess.StartInfo.UseShellExecute = False
clsProcess.StartInfo.RedirectStandardOutput = True
clsProcess.StartInfo.RedirectStandardError = True
clsProcess.StartInfo.FileName = "defrag"
clsProcess.StartInfo.Arguments = driveLetter & ": -a -v"
clsProcess.StartInfo.CreateNoWindow = True
' Check if system is Windows Vista or higher
If System.Environment.OSVersion.Version.Major >= 6 Then
clsProcess.StartInfo.Verb = "runas"
Else
' No need to prompt to run as admin
End If
clsProcess.Start()
clsProcess.WaitForExit()
Dim sLine As String = clsProcess.StandardOutput.ReadLine
Dim limit As Integer = 0
' Tried to skip every empty line at the beginning...
While sLine = Nothing
sLine = clsProcess.StandardOutput.ReadLine
limit += 1
If limit > 50 Or sLine <> Nothing Then
Exit While
End If
End While
limit = 0
Try
While Not sLine.Contains("Average fragments per file")
sLine = clsProcess.StandardOutput.ReadLine
limit += 1
If limit > 50 Or sLine Is Nothing Then
Exit While
End If
End While
sLine = sLine.Trim()
Catch ex As Exception
If sLine = Nothing Then
MsgBox("sline empty!!!")
End If
MsgBox(ex.ToString)
End Try
Thanks in advance!