Click here to Skip to main content
16,019,619 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to get the location of files are opening (For e.g: Word, Powerpoint, or Image ...) by using C# language. How I can do it, I have tried to find a solution but most of answer for project's current path.
To make my question clearly, I have a example as following.
I opening a picture with location: C:/pic.bmp. and a Powerpoint is : D:/data/present.pptx.
I want to write a program in winform C# to get these location include two string path: "C:/pic.bmp" and "D:/data/present.pptx".
Sorry for my bad english!
Thanks in advanced!
Posted
Updated 7-Jan-15 14:01pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Jan-15 16:18pm    
Why? Just curious. The problem is quite solvable, just a bit tricky.
—SA
Shaun Blain 7-Jan-15 16:54pm    
Your question is ambiguous the way it is written. Are you wanting the location of the where applications are installed or do you want to detect the applications currently running and then find their install locations?
Sergey Alexandrovich Kryukov 7-Jan-15 17:08pm    
The question is not the most clear, but some logical considerations help to understand it. As "current active application" is mentioned, it cannot be related to the installed but not running application. It's hard to imagine anything else except some running application with some its window active; also, it would be reasonable enough to suggest that the main window is active, because it makes the solution more feasible and enough for most practical purposes. I think I provided a relevant answer, please see; the problem could be a bit different, but my directions will be useful anyway.
—SA
Shaun Blain 7-Jan-15 17:26pm    
If I just went by the title, I would agree with you. The first sentence where it says by "using command on Winform" implies to me that he "might" be running an application and wants to know about the other running apps. Since English appears to this persons second ( or 3rd or 4th, etc.) language, then it is also fair to think that they may have mis-worded exactly what they are looking for. I am not knocking their language skills at all, it is simply an observation.

The solution you submitted is very complete if they are truly trying to perform exactly what you suggested. My hat is off to you for providing such a lengthy and detailed solution.

This request is somewhat weird. However, you can do it in the following way: get the HWND of the active (desktop-wide) main application window. Then list all the processes and retrieve the main window's HWND for those processes which have one. Traverse the set of such processes to compare this HWND with the HWND you found. When you find one, for the found process, locate the entry-point executable module of this process.

Let's start. To get this top-level window for a given main window, use the function GetLastActivePopup: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx[^].

In most cases, it will return the same main window handle, but we need to take into account the cases when some modal dialog is shown.

You may have a problem with the application which is executing some modal dialog on top of its main window. Then this dialog's HWND will be returned. Also, you need to know the owner window. Take the desktop HWND and list its children:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633494%28v=vs.85%29.aspx[^],
or http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx[^].

You can simply use GetWindows (see the last link above) with uCmd equal to GW_CHILD, using desktop as a parent, to retrieve the top child of the desktop in Z-order.

(Note: you cannot use Windows API GetActiveWindow, because it only can find the window if it is attached to the current thread, but your code's thread would be different.)

To use all these raw API functions, you will nee to use P/Invoke:
http://en.wikipedia.org/wiki/Platform_Invocation_Services[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^],
http://www.pinvoke.net/[^].

This CodeProject article can also be useful: http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].

Now, let's go to the processes side. This is how you can list all processes: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocesses%28v=vs.110%29.aspx[^].

When this is done, you can examine its main window handle: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle%28v=vs.110%29.aspx[^].

The method referenced above should be called under the try-catch block, because not all processes have this window. And this is the way this method works: it throws an exception when a window handle cannot be found.

In a loop, compare window handles with the handle of the found active window handle and thus locate the process in question. Now you can locate its main executable module and then find the module's file name:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainmodule%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule%28v=vs.110%29.aspx[^].

If you want, you can locate other modules of the active application found (if there are more of them) and do the same with each: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.modules(v=vs.110).aspx[^].

Now, you can face one difficulty: if your process has a windowed UI or just a console, it will grab activation on itself, so you will loose the information on what application was active before. Moreover, even if your application does not have any windows (not even a console window) and just writes the result in some file (just for example), the application which you use to start your process (say, any Windows Shell application, or anything else) would steel the active status. So, you need something else to defer the code you use to determine the active application. One way could be using a global hot key:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646327%28v=vs.85%29.aspx[^].

These two hot key functions can be used the same was as other Window API functions, via P/Invoke.

That's all. Wow, it's was a long description… :-)
It's possible that the algorithm I tried to depict could be improved and clarified, especially during actual development. I guess, you need to do your part of job.

—SA
 
Share this answer
 
v2
Comments
BillWoodruff 7-Jan-15 17:20pm    
+5
Sergey Alexandrovich Kryukov 7-Jan-15 17:20pm    
Thank you, Bill.
—SA
I can not solve my problem, please help me!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900