Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everybody i need a help with my VB.Net Code,

I would like to be able to list all files that are currently open by process for example "EXCEL" so i can get the files paths.

currently im using the following code which retrieve only the first file opened and the for each loop ends !!

What I have tried:

For Each p As Process In Process.GetProcessesByName(ProcessRuningName)
                Try
                    Using searcher As New ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " & p.Id)
                        

                     For Each mgmtObj As ManagementObject In searcher.Get()

                        cl = mgmtObj.Item("CommandLine").ToString().Split("""")
                         For x = 0 To cl.Count - 1
                          Dim FilePath As String = cl(x)
                          If System.IO.Path.GetFileName(FilePath) = FileName Then

                              processFilesRuning.Add(FileName)

                            End If

                            Next

                        Next

                    End Using

                Catch ex As Win32Exception

                    'handle error


                End Try
            Next


So as you can see im trying to search in each process with the name of "Excel" which is already retrevied but i get only one result which is the first opened Excel file and it ignore the other opened file so please guys help me with this.

Thanks in advance
Posted
Comments
Sandeep Mewara 6-Oct-20 18:01pm    
Found a similar problem discussed years back here: c# - find all open Excel workbooks - Stack Overflow[^]

Looks like should help.
Dave Kreskowiak 7-Oct-20 0:01am    
Well, I can tell you that the Win32_Process class only give you information about the Process object itself, not any files it has open.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900