I have a program that lists various files from a folder into a listbox control. That folder, which contains the files listed in the listbox, some of them are running on the computer but I want to delete all files available in that folder( and which are listed in the listbox). How can I firstly close the process or processes if more(but only if it they're running!), and after that, successfully delete that file or files. Do I need to get the process ID first ? (I don't want to close that process using the file name, because it is misleading, file names could be the same for more files and they're other files which I don't want to be deleted).
I have this to get process ID:
Dim aProcess As System.Diagnostics.Process
aProcess = System.Diagnostics.Process.GetProcessById(PID)
aProcess.Kill()
** Not sure how to get the process ID from the file(s) listed on the listbox which are actually from a folder, so I may need to get the process ID from file path like ("C:\ProgramFiles\TestApp\Test.exe", not only "Test.exe")
If the file is not running, I am able to successfully delete the file from that specific folder, like this:
Try
For Each z In Listbox1.Items
If My.Computer.FileSystem.FileExists(z) Then
My.Computer.FileSystem.DeleteFile(z)
Listbox1.Items.Remove(z)
Msgbox("The files have been successfully deleted!")
End If
Next
Catch Ex As Exception
End Try
But the only trouble is the kill process part. If the process is running, the file , obviously cannot be deleted. It has to be done from the full file path, not only "filename.exe"
Any help would be highly appreciated.