Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is some code:-
VB
Static OldWindow As String
Dim pr As Process, i As Integer

' add all running processes to collection for later use
prc = New Collection
For Each pr In Process.GetProcesses
   If pr.MainWindowTitle <> "" Then
      prc.Add(pr.MainWindowTitle)
   End If
Next
' now find process with focus
GetWindowThreadProcessId(GetForegroundWindow(), i)
pr = Process.GetProcessById(i)
Dim path As String = pr.MainWindowTitle                                                
If path <> OldWindow Then
   OldWindow = path
   RaiseEvent DocumentChanged(path)
End If

I was hoping not to use COM etc to find the path of the file loaded.
Any ideas?
Posted
Updated 9-Oct-12 5:34am
v2

1 solution

It's not very clear what you're asking for.

There is no generic method to find out which applications have which files loaded. Every application is different and may or may not expose this information at all. Most don't.

Now, if you're asking to see which files a process currently has open (for read and/or write, NOT "loaded"), then you have to enumerate the handles of the process. This requires administrative priv's. You can read about what it takes to do this here[^].
 
Share this answer
 
Comments
Stuart Nathan 9-Oct-12 12:01pm    
I am thinking of AutoCad, Word or Excel opening a file for read etc.
Obviously I can use COM, but this involves having different versions for different versions of Word.
I am also happy if an application does not return a file path.
I looked at the code but it is extremely complicated, and I really need to understand what it does.

I am also looking at Restart Manager (rstrtmgr.dll) but this is also complicated.
Dave Kreskowiak 9-Oct-12 13:14pm    
COM isn't going to work because not all applications expose an object model to use, and they would be different for every application, so you'd have to write code for every single application you want to get the loaded file from.


Reading the TitleBar text isn't going to work either. It'll work for some applications, but there are lots of applications that don't add the file they opened to the title bar text nor does every application put the complete path to the file it has open in there.

Some application windows don't even respond to the WM_GETTEXT message.

In short, there is no method at all that will give you the currently loaded file for every application.
Stuart Nathan 9-Oct-12 13:18pm    
What about MS Office? Can I do that at least?
Dave Kreskowiak 9-Oct-12 21:41pm    
The best way to do that is through the COM interface. I highly suggest taking a look at the Visual Studio Tools for Office.

http://msdn.microsoft.com/en-us/office/hh133430.aspx

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