[英]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.