I'm writing a small app to collect IE command diag. The problem is it's not waiting till the process finishes and it's errors out in the next line. My goal is to copy the log and paste it under a folder in Desktop

Try
            Process.Start("C:\Program Files\Internet Explorer\iediagcmd.exe")
            My.Computer.FileSystem.CopyFile(USERPROFILE & "\Desktop\IEDiag.cab", USERPROFILE & "\Desktop\W8\IEDiag.cab",
            Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
            Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
            My.Computer.FileSystem.DeleteFile(USERPROFILE & "\Desktop\IEDiag.cab")
            MsgBox("IE Logs copied sucessfully")
        Catch ex As Exception
            MsgBox("Error copying file: " & ex.Message)
        End Try

What am I doing wrong. And please bear in mind i'm a rookie in VB.

Thanks,

Dani AI

Generated

Nice catch by and good fix by : WaitForExit must be called on the same Process instance you started. To make this reliable across 32/64-bit Windows and redirected Desktops, avoid hard‑coded paths and HOMEPATH concatenation. Use special folders and Path.Combine, check the exit code, and only copy once the CAB exists.

Dim exe = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
                          "Internet Explorer\IEDiagCmd.exe")
If Not IO.File.Exists(exe) Then
    exe = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                          "Internet Explorer\IEDiagCmd.exe")
End If

Dim desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Dim src = IO.Path.Combine(desktop, "IEDiag.cab")
Dim destDir = IO.Path.Combine(desktop, "W8")
IO.Directory.CreateDirectory(destDir)
Dim dest = IO.Path.Combine(destDir, "IEDiag.cab")

Using p As New Process()
    p.StartInfo.FileName = exe
    p.StartInfo.UseShellExecute = False
    p.Start()
    If Not p.WaitForExit(300000) Then Throw New TimeoutException("IEDiagCmd timed out.")
    If p.ExitCode <> 0 Then Throw New Exception("IEDiagCmd failed with exit code " & p.ExitCode)
End Using

For i = 1 To 20
    If IO.File.Exists(src) Then Exit For
    Threading.Thread.Sleep(250)
Next

IO.File.Copy(src, dest, True)
IO.File.Delete(src)

Notes:

  • Process.WaitForExit() blocks the current thread until the process ends (use the Exited event if you need to keep the UI responsive, as showed). (learn.microsoft.com)
  • Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) resolves the real Desktop path (handles redirection and avoids HOMEDRIVE/HOMEPATH pitfalls). (learn.microsoft.com)

These tweaks should prevent the copy from racing the diagnostic tool and make the script resilient on different machines.

Recommended Answers

All 3 Replies

There are two ways to do it. First is to wait

copyProcess.Start("C:\Program Files\Internet Explorer\iediagcmd.exe")
copyProcess.WaitForExit(10000) ' Wait until finished or timeout after ten secs

for the process to exit.

Second is to wait event which a process raises when finished:

Private WithEvents copyProcess As Process ' Declare process

' Event handler for process exit
Private Sub copyProcess_Exited(sender As System.Object, e As System.EventArgs) Handles copyProcess.Exited
      ' Process finished
      ' Do the copy here after process finished
End Sub

' I started process from a button click but you can do it in some other way
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click

    ' Enable events
    copyProcess.EnableRaisingEvents = True
    ' Start process
    copyProcess.Start("C:\Program Files\Internet Explorer\iediagcmd.exe")

End Sub

HTH

commented: Thanks for the help. I can alway count on this community. +1

I'm trying the first method. But I get an error "Reference to a non shared member requires an object reference

here is the code:

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Dim USERPROFILE As String
        USERPROFILE = Environ("HOMEPATH")
        IO.Directory.CreateDirectory(USERPROFILE & "\Desktop\W8")
        Dim strSysRoot As String
        strSysRoot = Environ("SystemRoot")
        USERPROFILE = Environ("HOMEPATH")
        Try
            Process.Start("C:\Program Files\Internet Explorer\iediagcmd.exe")
            Process.WaitForExit(10000)
            My.Computer.FileSystem.CopyFile(USERPROFILE & "\Desktop\IEDiag.cab", USERPROFILE & "\Desktop\W8\IEDiag.cab",
            Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
            Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
            My.Computer.FileSystem.DeleteFile(USERPROFILE & "\Desktop\IEDiag.cab")
            MsgBox("IE Logs copied sucessfully")
        Catch ex As Exception
            MsgBox("Error copying file: " & ex.Message)
        End Try

Sorry got it to work.

Declared it first
Dim myProcess As Process = System.Diagnostics.Process.Start("C:\Program Files\Internet Explorer\iediagcmd.exe")

Then used myProcess.WaitForExit()now it works

Thanks for the help mate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.