Get Filename of the Current Active Window - c#

I am trying to get the filename of a word document or any other window for that matter while some dialog box like Save as or Open or Print etc. is open. Can anybody give me an example of how to implement this in C#.

Windows API can give you the name of the application which occupies a particular window, but to know what file (or files) are open within that application would require OLE automation or some other API supplied by the application (here winword.exe) itself.

Related

How does Windows Photo Viewer get the files for the slideshow from Windows Explorer?

I wrote an image viewer application in C# to replace Windows 7 Photo Viewer which does not have the one feature I needed, which was to set the ratings and keywords directly from the view screen and not by opening some less user-friendly property pages. It works Ok for my need, but I wanted to improve it with a slideshow.
With Windows 7 Photo Viewer, what I usually did was I set the keywords of the picture, I used Windows Explorer "organize by" feature, clicked one keyword, double-clicked one file and run the slideshow from there.
But when I double-click a file from the keyword-"organized" folder in Windows Explorer, all my app gets is the command-line argument, i.e. the full name of the file.
I could admit running my slideshow from there, using System.IO.Path.GetDirectoryName to get all files from the folder, but I find it lacks a certain "panache", don't you think?
I am not asking for code here. But could someone just point me in the right direction, please? I don't know what WindowsExplorer actually gives PhotoViewer that allows it to only show the required files. And why.
Thanks for any help.
When you use Organize by feature of Windows Explorer it creates special virtual folder. And this folder contains files with selected keyword only. Mechanism of opening of PhotoViewer differs from standard way. If you open HKEY_CLASSES_ROOT\jpegfile\shell\open registry key you will see that where is DropTarget subkey. It means when you double-clicked the file shell creates inproc com server with CLSID from DropTarget subkey and pass virtual path of image to IDropTarget instance. So PhotoViewer work with list of virtual objects instead of physical directory.

How can I determine the name of the file opened in the currently focused process

For example, if the currently focused process is Microsoft Word, and I have opened in it a document called myFile.doc, I know how to get the window text (in this case could be "myFile.doc - Word") and I know how to get the process name ("winword") or the process description ("Microsoft Word").
Is there any way to get only the name of the file opened in the currently focused process? It can be any process.
I know that I can parse the window text and get the opened file, but for example, in spanish version of Notepad the window text format is different ("myFile.txt: Bloc de notas").
I think that maybe there is a way to get aditional details about the currently focused process.
Thanks in advance!

How to read out text present on external application dialog box or window in C#

I want to required file name of my currently opened document application(like pdf,docx,xls etc) in order to automatically detect the password from my access database.
But when i clicked on my external application the "password" window gets opened since my file application is passwords protected, still file application not opened because i have not entered password still, i want to supply this password as auto detection from database.
For this project i have got the title of currently active window i.e. password but i want to know the which file name, of application.
The filename with extension is present on external application(pdf,docx,xls)"enter password" dialog box or window.
So my question is that is there any way to read out this "filename+extension " present on "enter password" dialog box of external application(like pdf,docx,xls etc).
You need to access the Windows API through PInvoke for this.
First, you need to get the handle of the window you are interested in, look at this for some example code.
Then you can get the text from the title bar using this method described here.
Hopefully this should be enough to get you started.

Opening an image file in an executable I created

I just finished this tutorial - Create a Picture Viewer - on the MSDN site, and it all works well according to the tutorial specifications (although I did remove the unnecessary buttons).
At the moment, I can open an image file from within the program, which is fine.
However, I'd like to be able to open an image file from Windows Explorer, and have it open in my Image Viewer. (Using the Open With.. context menu).
I did try and open it via the Open With.. menu, but when the program loaded, the image didn't show up. The program just started up as it normally would.
What code do I need to put in, that allows me to open the program via an image file (if that makes sense)
You can use:
Environment.CommandLine
Which will contain stuff in the format of "..." "...", first being the path of your application, and in your case - second would be the path of the opened-with file.
Then, you can split that to get the second "..." and load the file as you normally do in you application.
You can also check to see whether the arguments of the application contain the path of the opened-with file. I'm not sure about that, but it should be very easy to check: Have an mbox which prints the parameters, then try opening a file with your program and see what shows up. Using the args[0] or args[1] or whatever will probably be easier than splitting Environment.CommandLine...

.Net multiple app instances but single document

My WPF app can open and edit single documents. I am looking for a tidy approach to allow multiple instances of my WPF app to run but to only allow a given document to be open in one instance of the app. If a user tries to open a document which is already opened in another instance, I need to pop up a dialog to tell them and allow them to switch to the other app instance if required.
Thanks
Dan
One approach is to attempt to take an exclusive lock on the file when you open it. When your other application instance attempts to open the file, an IOException will be raised. You can catch this exception and show a dialog to your user stating that the file is already opened in another application. This scenario should be covered anyway, as the file could be open in a different application that is not yours.

Categories