简体   繁体   English

如何在WinForm C#中获取当前正在运行的应用程序名称和ID?

[英]How can i get Current running Application name and ID in WinForm C#?

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. 在这里,我获得了ProcessID ,它正在工作。 But I want I to find the Application Name and ID. 但是我想找到应用程序名称和ID。

How can I get Running Application names with ID and how to pass Id to `Win32_Process Table. 如何获取带有ID运行应用程序名称,以及如何将Id传递到Win32_Process表。

I added win32_proceess for getting processid but we trace the application Id 我添加了win32_proceess以获取processid,但我们跟踪应用程序ID

You can get the application id and name using a method of the process class: 您可以使用流程类的方法获取应用程序ID和名称:

System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
int id = p.Id;
string name = p.ProcessName;

投票支持乔纳森(Jonathan),但想添加另一种方式(只要您不使用一次单击即可)

System.AppDomain.CurrentDomain.FriendlyName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中获取当前应用程序的名称 - How to get the name of current application in C# 如何在C#中获取当前(WLAN)网络的名称? - How can I get the name of the current (WLAN) network in C#? 如何在 C# 中获得两列 winform 列表框? - How Can i get Two column winform listbox in C#? 如何在 c# 中以编程方式编译 ac# winform 应用程序 - How can i compile a c# winform application programmatically in c# 如何在Chartarea C#Winform中获得当前scaleView中的点? - How to get the points in the current scaleView in chartarea c# winform? 如何在C#中获取当前方法的名称及其参数(名称和值) - How Can I Get Current Method's Name And Its Arguments(name and value) in C# 如何获取当前Service Fabric应用程序的名称? - How can I get the name of the name of the current Service Fabric application? 当我在 C# 中有多个 datagridviews 时,如何获取当前选定的 datagridview 的名称? - How can I get the name of the current selected datagridview when I’ve multiple datagridviews in C#? 如何获取当前用户的ID(在asp.net/c#网站中)? - how can I get the current user's id (In an asp.net/c# website)? 如何在Winform应用程序中获取Excel工作表的行和列ID - How can I get row and column id of an Excel sheet in my winform application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM