Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello every one,

How can I find the application which is currently running application using C#?

Scenario: I m developing C# WinForms application...I found Current Running Process name..But I want to display current running application path..See this below code

C#
public class SmallWork
       {
       [DllImport("user32.dll")]
       static extern IntPtr GetForegroundWindow();

       [DllImport("user32.dll")]

       public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);



       public static string GetActiveProcessFileName()
       {
           try
           {
               IntPtr hwnd = GetForegroundWindow();
               uint pid;
               GetWindowThreadProcessId(hwnd, out pid);
               Process p = Process.GetProcessById((int)pid);
               // return p.MainModule.FileName;
               string CommandLine = GetMainModuleFilepath((int)pid);

               var array = CommandLine.Split('"');

            }

       }

      public static string GetMainModuleFilepath(int processId)
       {
           string wmiQueryString = "SELECT * FROM Win32_Process WHERE ProcessId = " + processId;
           using (var searcher = new ManagementObjectSearcher(wmiQueryString))
           {
               using (var results = searcher.Get())
               {


                   ManagementObject mo = results.Cast<ManagementObject>().FirstOrDefault();
                   if (mo != null)
                   {
                       return ((object)mo["CommandLine"]).ToString();
                   }
               }
           }
           //Process testProcess = Process.GetProcessById(processId);
           return null;
       }

Problem: If I opened three MS-WORD files ,Then it display only First opened file name..similarily MS-EXCEL and PDF pages also..Because Every word or Excel or Pdf Contains Only One Process..But i want to shows Every Opened Document or application names saving with Path...How can i Achive this..

Thank you all for the answers and support in advance..
Posted
Comments
Pikoh 3-Feb-15 3:31am    
So,just to clarify. What you want is to find the name of all the documents opened in, let's say, all instances of Word opened?
santhu888 3-Feb-15 3:46am    
Yes,I want to find all opened documents file path names
Sergey Alexandrovich Kryukov 3-Feb-15 3:45am    
There is no such concept as "current process". They all are "current" enough. You are trying to find the one with foreground window (note there is also an active window). Your requirement "to show every open document" apparently contradict with it. Can you formulate your requirement strictly, taking into account what I just told you?
—SA
santhu888 3-Feb-15 3:54am    
It's Like Task Manager window..If i Click Process Tab..It shows running Process names as well as If i click on Application Tab ,Then it shows Every opened Document Names with path...

1 solution

This works for me:

Add a reference to Microsoft.Office.Interop.Word

Then add this usings:

C#
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;


And finally, use this method:

C#
private List<string> GetDocumentPaths()
        {
            List<string> Documents= new List<string>();
            Word.Application wdapp;
            wdapp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
            foreach (Document d in wdapp.Documents)
            {
                Documents.Add(Path.Combine(d.Path,d.Name));
            }
            return Documents;
        }
 
Share this answer
 
v2

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