Question delete a temp directory after a program closes

madhatter5501

New member
Joined
Jun 29, 2012
Messages
2
Programming Experience
Beginner
I am developing a kind of portable toolkit for learning purposes and am stuck on the following code. If anyone understands what I am trying to do, and can assist a newbie would be great!

The part in particular that I am stuck on is running the Do While loop to check if the "firefox.exe" process is running and if it is running - do a thread.sleep and loop. If the process is not running, delete the temporary directory created.

VB.NET:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        FireFox()

End Sub

Sub FireFox()

        Dim programName As String = "FirefoxPortable.exe"
        Dim tempworkingdirectory As String = "C:\temp-working-directory\"
        Dim firefoxdirectory As String = tempworkingdirectory & "FirefoxPortable\"
        Dim userInput

        userInput = MsgBox("Please wait while firefox is being copied locally to your hard drive" & vbCrLf & "Firefox will be located here: " & firefoxdirectory & vbCrLf & "Press OK to continue!", vbOKCancel, "Information")
        Try
            Select Case userInput
                Case vbOK
                    If My.Computer.FileSystem.DirectoryExists(firefoxdirectory) Then
                        My.Computer.FileSystem.DeleteDirectory(firefoxdirectory, FileIO.DeleteDirectoryOption.DeleteAllContents)
                    End If
                    My.Computer.FileSystem.CreateDirectory(firefoxdirectory)
                    My.Computer.FileSystem.CopyDirectory(filePath & "FirefoxPortable\", firefoxdirectory)
                    ProgramRun(firefoxdirectory, programName)
                Case vbCancel
                    'Do Nothing
            End Select


            Do While CheckProcess("FireFox.exe") = True
                Select Case CheckProcess("Firefox.exe")
                    Case True
                        MsgBox("FireFox is still running", vbOKOnly)
                        System.Threading.Thread.Sleep(500)
                    Case False
                        MsgBox("FireFox has closed!", vbOKOnly)
                        My.Computer.FileSystem.DeleteDirectory(firefoxdirectory, FileIO.DeleteDirectoryOption.DeleteAllContents)
                End Select
            Loop

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

 Public Function CheckProcess(name As String) As Boolean
        For Each clsProcess As Process In Process.GetProcesses()
            If clsProcess.ProcessName.StartsWith(name) Then
                Return True
            End If
        Next
        Return False
    End Function
 
Thanks for the direction, here is what I came up with and it seems to work good so far

VB.NET:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim t As Threading.Thread
        t = New Threading.Thread(AddressOf Me.FireFox)
        t.Start()

End Sub

Sub FireFox()

        Dim tempworkingdirectory As String = "C:\temp-working-directory\"
        Dim firefoxdirectory As String = tempworkingdirectory & "FirefoxPortable\"
        Dim userInput

        userInput = MsgBox("Please wait while firefox is being copied locally to your hard drive" & vbCrLf & "Firefox will be located here: " & firefoxdirectory & vbCrLf & "Press OK to continue!", vbOKCancel, "Information")
        Try
            Select Case userInput
                Case vbOK

                    Try

                        Dim objProcess As System.Diagnostics.Process
                        Dim programName As String = "FirefoxPortable.exe"

                        If My.Computer.FileSystem.DirectoryExists(firefoxdirectory) Then
                            My.Computer.FileSystem.DeleteDirectory(firefoxdirectory, FileIO.DeleteDirectoryOption.DeleteAllContents)
                        End If

                        My.Computer.FileSystem.CreateDirectory(firefoxdirectory)
                        My.Computer.FileSystem.CopyDirectory(filePath & "FirefoxPortable\", firefoxdirectory)

                        objProcess = New System.Diagnostics.Process
                        objProcess.StartInfo.FileName = (firefoxdirectory & programName)
                        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
                        objProcess.Start()

                        'Wait for exit
                        objProcess.WaitForExit()
                        'MsgBox("Firefox has exited")

                        'Free resources associated with this process
                        objProcess.Close()
                    Catch ex As Exception
                        MsgBox(ex.ToString)
                    End Try

                Case vbCancel
                    'Do Nothing
            End Select

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
End Sub
 

Latest posts

Back
Top