Link to home
Start Free TrialLog in
Avatar of slm999
slm999

asked on

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!

--Steve
Avatar of graye
graye
Flag of United States of America image

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.
Avatar of slm999
slm999

ASKER

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:

                System.Diagnostics.Process.Start(ProgramAwaitingTimer)

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.]

THANKS FOR YOUR HELP!!!

--Steve
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slm999

ASKER

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,
Steve

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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassmainwindowtitletopic.asp

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.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/sendmessage.asp
Avatar of slm999

ASKER

graye,

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.

--Steve