How do I look at the paths and file names of open windows? 500 points!!

Hi Experts!

I need to find the window that has a user-specified file open, and make that window the active and top-most window.

Note that I won't know what the process running the file will be.  For instance, the file might be a .txt file open in  MS-Word rather than Notepad.

Thanks for your help!

Who is Participating?

Improve company productivity with a Business Account.Sign Up

grayeConnect With a Mentor Commented:
Yeah, that program use a lot of dark voodoo...   so don't be hard on yourself it it doesn't make too much sense.   Any program that uses "native" undoucmented APIs is not for the faint of heart.

But even that program won't do what you want (for the reasons I've explained above).  Using NotePad as an example, the file that notepad has open, isn't really open!  It was opened for a fraction of a second, loaded into memory, and then closed.   It stays closed during the entire editing session and only gets opened again (for a fraction of a second) when the user saves the currently cached file back to disk.  So the OpenFile demo will not be able to detect an file that NotePad has "open" (because it isn't open!)

So, let's talk about the design of your program...   Is it possible to trigger the action (whatever that might be) based upon the file being modified?  In other words would it work to have the program wait until the file as been written to before leaping into action?
That's actually very difficult to do...

The problem is that files might not be held open except during the initial loading and saving of the file (for example, NotePad and Word do not hold files open during editing).   In those cases, the only way to associate a running process with the file that it is editing, is by looking at each process for some clues.

Thankfully most program include the name (but not the full path) of the file being edited in the title bar.   If that's suitable for your needs, it's kinda simple to query all of the user's windows and find the one that contains the text in the title bar that matches the file you're after.

Let us know if we're on the right track, and we can provide some examples/pointers.
slm999Author Commented:
Thanks, but sometimes I need the full file and pathname.  I can't just use the file name, because it mightt be a different file from a different folder and/or drive but with the same filename.

Oh, WOW, you're graye.  I found your answer to Karen Huong and the demo program that you wrote back in September.  Unfortunately, I don't quite have the VB.NET experience to follow the entire solution (I got out of systems and programming back in 1985).  Let me tell you what I'm doing and then possibly you can help me through your code.

My user selects a file that will be opened at some future time.  Here is what my users (and I) want to happen when it is time to open the user's file: if the file is not already open, they want it opened as the top-most window with focus; and if the file is opened, they want the window with that file to be moved to top-most with focus.  

To open the file, I use:


For most programs, including Notepad and Word, there is no problem.  In Word, when I try to start an open file, it just brings the already open window to the top and gives it focus, WHICH IS GREAT.  Notepad, however, opens a new instance with another copy of the file, which is okay.  However, a problem arises when my program tries to open an Excel file that is already is open and has been modified since the last save.  Excel asks whether the user wants to revert back to the saved version of the file.  I'm sure that this would happen with other programs, as well.  This message can be confusing for my users.  Is there any way to know which other programs would act like Excel?

Now, in your program:
•      I don't understand the need for and how you found the proper buffer size and then you added a little padding
•      There are a lot of other things in the program that I don’t understand and I am wondering how much and which of it I need if I already know the path and filename and just trying to find the handle of the proper window [Remember, I can’t just check the window text, because it doesn’t guarantee that it is the same file that the user specified.]


Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

slm999Author Commented:
Thanks for the quick response.

My program is similar to MS Windows Scheduled Tasks in that the user specifies a file and a time and/or time interval at which to open the file.

At this point, Excel is the only program with which I've found the problem.  I will check how the System.Diagnostics.Process.Start(ProgramAwaitingTimer) command works with some of the other software that I expect users to use.  I won't be able to check them all because I don't and can't know them all.  

***  Is there any way to know which programs will work like Excel, with the program asking the user if they want to revert to a saved version of the open file, or asking if they want to open the file "read only"?  ***

Though I would not like doing it, I can send out my program and let the users come back to me and say that it doesn't work with programs LMN, TUV, and XYZ.  That seems like the Microsoft method, and I've always faulted them for it.

Being a perfectionist, what I want to do for an Excel file is:
-- check if the file is open
-- if open, look for the Excel window with the proper text
-- check the path for that window
-- if it is what I'm looking for, then make top-most and give focus
-- if it is not the right path, keep checking other Excel windows until I find the one with the right path
-- if I don't find an Excel window with the proper text, I will assume that the file is open by a program other than Excel and give the user a messagebox just stating that it is time for my program to open their file, but the file is currently open

If I can somehow live with less than perfection (and software, like golf, is not a game of perfect), I might consider one of the following:
Almost perfect for an Excel file is:
-- check if the file is open
-- if open, look at ALL Excel windows for the proper text
-- if only one Excel window has text I'm looking for, then make top-most and give focus
-- if more than one Excel window has text I'm looking for, then make one of them top-most with focus and give user a messagebox stating that it is the right filename but might not be the path he specified

Much less perfect for an Excel file is:
-- check if the file is open
-- if open, look for the Excel window with the proper text
-- assume that this is the right file and make it top-most window with focus

Which of these solutions would you suggest?

Thanks again for your help,

Finding the window that has the text in the title bar is kinda easy... just loop thru the processes and use the MainWindowTitle property to see if you've got a match

To force an external window (a process) to become a foreground task, I'd consider using the SendMessage API to send a "fake" mouse click event to the MainWindowHandle property.
slm999Author Commented:

Thanks for your guidance!  I've read so much about the MainWindowTitle property and the handles while trying to solve the path problem that I should be able to handle it from here.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.