0
public static string GetActiveProcessFileName()
{        
   try
    {
        IntPtr hwnd = GetForegroundWindow();
        uint pid;
        GetWindowThreadProcessId(hwnd, out pid);
        Process p = Process.GetProcessById((int)pid);

        CommandLine = GetMainModuleFilepath((int)pid);

    catch (Exception ex)
    {
        ErrorLog.ErrorLog.Log(ex);
        return "Explorer";
    }
}

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


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

Here I get ProcessID and it's working. But I want I to find the Application Name and ID.

How can I get Running Application names with ID and how to pass Id to `Win32_Process Table.

I added win32_proceess for getting processid but we trace the application Id

ramaraog
  • 57
  • 2
  • 15

2 Answers2

4

You can get the application id and name using a method of the process class:

System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
int id = p.Id;
string name = p.ProcessName;
Jonathan Camilleri
  • 611
  • 1
  • 6
  • 17
  • Thanks for reply ..Now i want to pass this id SELECT * FROM Win32_Process WHERE = " + id; In our Win32_Process does not have ApplicationID properity.. – ramaraog Feb 02 '15 at 08:20
  • I am assuming that by passing the id you mean getting the process from the Win32_Process table. Here is a link to codeproject for similar use: http://www.codeproject.com/Articles/18146/How-To-Almost-Everything-In-WMI-via-C-Part-Proce however it is noted that using WMI is slower than using the Process provided by Process.GetCurrentProcess() – Jonathan Camilleri Feb 02 '15 at 08:27
  • Is it ok..Finally is it possible to get the Application Id SELECT * FROM Win32_Process WHERE ProcessId = " + ID – ramaraog Feb 02 '15 at 08:36
  • You sent correct answer ..It's used for me..Now i have one problem..wmiQueryString = "SELECT * FROM Win32_Process WHERE ProcessId = " + ApplicationID; In this query i entered Application ID..Now i get every file with exe name....But i want to display with Application Name – ramaraog Feb 02 '15 at 11:05
  • Here are the columns of Win32_Process https://msdn.microsoft.com/en-us/library/aa394372%28v=vs.85%29.aspx you can try using the "Name" column for the query – Jonathan Camilleri Feb 02 '15 at 11:10
  • Yes..I tried it..Now it's Saving the Application Name..For example If i opened Excel..It shows "Excel.exe"...But i want to display File Name with saving File Name ,Like About.xlsx..if i opened word with Contact.docx name...Is it possible to find any way.. – ramaraog Feb 02 '15 at 13:47
  • Win32_Process does not support it, but if you use the Process, you can use the Main Window Title variable – Jonathan Camilleri Feb 02 '15 at 13:52
  • Yes,We can get the Main Window Title..Is it possible to find the Total Path of the Opened Application Document? – ramaraog Feb 02 '15 at 14:25
  • For that you need to see what files the process has open.... here is a link to another so question for that http://stackoverflow.com/questions/177146/how-do-i-get-the-list-of-open-file-handles-by-process-in-c – Jonathan Camilleri Feb 02 '15 at 14:34
0

Voted for Jonathan's but would like to add another way (as long as you're not using click once)

System.AppDomain.CurrentDomain.FriendlyName
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99