Solved

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

Posted on 2006-06-25
6
191 Views
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!

--Steve
0
Comment
Question by:slm999
  • 3
  • 3
6 Comments
 
LVL 41

Expert Comment

by:graye
Comment Utility
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.
0
 

Author Comment

by:slm999
Comment Utility
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
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
Comment Utility
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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:slm999
Comment Utility
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

0
 
LVL 41

Expert Comment

by:graye
Comment Utility
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
0
 

Author Comment

by:slm999
Comment Utility
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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now