[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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

Posted on 2006-06-25
Medium Priority
Last Modified: 2010-04-23
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!

Question by:slm999
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 41

Expert Comment

ID: 16980947
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.

Author Comment

ID: 16986188
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.]


LVL 41

Accepted Solution

graye earned 2000 total points
ID: 16986861
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?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 16988344
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,

LVL 41

Expert Comment

ID: 16992073
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.


Author Comment

ID: 16996359

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.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question