0

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.

preciousbetine
  • 2,959
  • 3
  • 13
  • 29
  • Does this answer your question? [How to get the full path of running process?](https://stackoverflow.com/questions/5497064/how-to-get-the-full-path-of-running-process) – GSerg Sep 02 '20 at 08:57
  • Unfortunately, no. First, that is C# version, I need VB.NET version. Also, even so, I looked up on it, it wouldn't help either. I know it is something related to API architecture, but not sure how to do that. If I do it using only filename "test.exe" I am able to close it, but IF there are more "test.exe" files running on the PC(and which are NOT in that folder), I don't want to close those, but the only 'filename' method also closes those. I want to close process using full path. I already have those files listed in listbox with the full path. – Andrew Mickelson Sep 02 '20 at 09:40
  • So, store in your ListBox the full path of the executables you listed from that source Directory. What problem do you have with that? – Jimi Sep 02 '20 at 09:45
  • I am able to successfully store the files from that folder in the listbox. I am unable to KILL the process IF a file is running from that folder? – Andrew Mickelson Sep 02 '20 at 09:55
  • The "Does this answer your question?" line is generated automatically by this website. It was not really a question of whether it answers your question because I know that it does. It does not matter whether you call the .NET methods from VB or C#, they are the same methods that are called the same way. [continued] – GSerg Sep 02 '20 at 09:55
  • The C# question tells you how to get the full path of a certain `Process` object. You know how to [get all `Process` objects with a certain exe name](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.getprocessesbyname?view=netcore-3.1#System_Diagnostics_Process_GetProcessesByName_System_String_). So you can filter the result of the latter to only keep those text.exe `Process` objects that also have the correct full path. [continued] – GSerg Sep 02 '20 at 09:56
  • Alternatively, you can scroll to the [next answer](https://stackoverflow.com/a/5497319/11683) and use the WMI query, in the exactly same way, and add `where ExecutablePath = 'C:\ProgramFiles\TestApp\Test.exe'` to the `select` query. – GSerg Sep 02 '20 at 09:59
  • And how do I KILL a certain process once I have the files listed in the listbox with their full paths? I don't seem to find any answers on this. I already have the full path of the files listed in the listbox, I do need something else, the kill process from full path. – Andrew Mickelson Sep 02 '20 at 10:08
  • GSerg already explained the whole process. You have the path of an executable (don't call it *file*). Search for a Process that has a corresponding `MainModule.FileName`. You can find it using `Process.GetProcesses()` or using a WMI query (which requires admin privileges, though). Or Win32 functions (really not necessary here). Note that some Processes can throw when you try to read their `MainModule` property, so try/catch that code. – Jimi Sep 02 '20 at 10:27
  • 1
    You can write something like: `using (var proc = Process.GetProcesses().FirstOrDefault(p => p.Id > 4 && p.MainModule.FileName == [Your ListBox Item])) { if (proc != null) { proc.Kill(); } }` – Jimi Sep 02 '20 at 10:35
  • 1
    *that is C# version, I need VB.NET version* - you are aware that the two languages are the same thing, right? – Caius Jard Sep 02 '20 at 11:05
  • 1
    `Using proc As Process = Process.GetProcesses().FirstOrDefault(Function(p) p.Id > 4 AndAlso p.MainModule.FileName = "[Your ListBox Item]") If proc IsNot Nothing Then proc.Kill() End Using` – Jimi Sep 02 '20 at 11:13
  • I've added it like that: Using proc As Process = Process.GetProcesses().FirstOrDefault(Function(p) p.Id > 4 AndAlso p.MainModule.FileName = Listbox2.SelectedItem.ToString) If proc IsNot Nothing Then proc.Kill() End If End Using * and I've tested it, I selected an item from Listbox2, but file is still running.. – Andrew Mickelson Sep 03 '20 at 18:41
  • Error as follows: 'A 32 bit processes cannot access modules of a 64 bit process.' Also, after disabling "Prefer 32-bit "from "Compile" settings, retrieved "Access is denied." – Andrew Mickelson Sep 03 '20 at 18:47
  • No further reply/help ? – Andrew Mickelson Sep 06 '20 at 12:48

0 Answers0