How to Get Active Process Name in C#?

后端 未结 5 441
失恋的感觉
失恋的感觉 2020-12-01 06:43

How to get active process name in C#?

I know that I must use this code:

[DllImport(\"user32.dll\")]
private static extern IntPtr GetForegroundWindow(         


        
相关标签:
5条回答
  • 2020-12-01 07:17

    As mentioned in this answer, you have to use GetWindowThreadProcessId() to get the process id for the window and then you can use the Process:

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);
    
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    
    string GetActiveProcessFileName()
    {
        IntPtr hwnd = GetForegroundWindow();
        uint pid;
        GetWindowThreadProcessId(hwnd, out pid);
        Process p = Process.GetProcessById((int)pid);
        p.MainModule.FileName.Dump();
    }
    

    Be aware that this seems to throw an exception (“A 32 bit processes cannot access modules of a 64 bit process”) when run from a 32-bit application when the active process is 64-bit.

    EDIT: As Damien pointed out, this code is prone to race conditions, because the process that had the active window at the time when GetForegroundWindow() was called might not exist anymore when GetWindowThreadProcessId() is called. Even worse situation would be if the same hwnd would be assigned to another window at that time, but I guess this should be really rare.

    0 讨论(0)
  • 2020-12-01 07:25

    Its just need two line of code, you can use linq to get all processes.

    var processss = from proc in System.Diagnostics.Process.GetProcesses() orderby proc.ProcessName ascending select proc;
    foreach (var item in processss) {
        Console.WriteLine(item.ProcessName );
    }
    

    Now you have all active process by just on line.

    0 讨论(0)
  • 2020-12-01 07:30

    Here's a link describing the exact thing you want to do:

    http://www.blackwasp.co.uk/GetActiveProcess.aspx

    And another one describing the GetForegroundWindow function, which I copy below.

    Note that you may need to reference some extra assemblies, for this code to work. Look at the MSDN for each function. Example, GetProcessesByName requires System.Diagnostics.

    public ApplicationState AppState
    {
        get
        {
            Process[] processCollection =
                               Process.GetProcessesByName(ProcessName);
            if(processCollection != null && 
               processCollection.Length  >= 1 && 
                processCollection[0] != null)
            {
                IntPtr activeWindowHandle = Win32.GetForegroundWindow();
                // Optional int ProcessID;
                // Optional Win32.GetWindowThreadProcessId(
                                                     GetForegroundWindow(), 
                                                     out ProcessID)
                foreach(Process wordProcess in processCollection)
                {
                    //Optional if( ProcessID == wordProcess.Id )
                    //          return ApplicationState.Focused;
                    if(wordProcess.MainWindowHandle == activeWindowHandle)
                    {
                        return ApplicationState.Focused;
                    }
                }
    
                return ApplicationState.Running;
            }
    
            return ApplicationState.NotRunning;
        }
    } 
    
    0 讨论(0)
  • 2020-12-01 07:31
        public void GetProcessNames()
        {
            List<string> windowNames = new List<string>();
    
            foreach (Process window in Process.GetProcesses())
            {
                if (window.MainWindowHandle != IntPtr.Zero)
                {                    
                    windowNames.Add(window.MainWindowTitle);
                }
    
                // It's that simple
            }
        }
    
    0 讨论(0)
  • 2020-12-01 07:41

    I would suggest using System.Diagnostics.Process.

    var currentProc = System.Diagnostics.Process.GetCurrentProcess();
    string name = currentProc.ProcessName;
    

    As an alternative you could use:

    string name = currentProc.MainModule.FileName;
    
    0 讨论(0)
提交回复
热议问题