[英]How to get the name of program running on windows with c#
I want a function that give me a name of program(like msPaint or notepad) that when i work with notepad, this function return "Notepad" , when i work with msPaint, this function return "msPaint". 我想要一个给我程序名称的函数(例如msPaint或notepad),当我使用记事本时,此函数返回“ Notepad”,当我使用msPaint时,此函数返回“ msPaint”。
Please help me... 请帮我...
Using Process.GetProcesses();
使用
Process.GetProcesses();
method you can get all running application and if you want current active window then make use of GetForegroundWindow()
and GetWindowText()
. 方法,您可以获取所有正在运行的应用程序,如果需要当前的活动窗口,请使用
GetForegroundWindow()
和GetWindowText()
。
for example click here 例如点击这里
You can use the Process class. 您可以使用Process类。 It has a Modules property that lists all the loaded modules.
它具有一个Modules属性,该属性列出了所有已加载的模块。
To list all processes and all modules to the console: 要将所有进程和所有模块列出到控制台:
Process[] processes = Process.GetProcesses();
foreach(Process process in processes) {
Console.WriteLine("PID: " + process.Id);
Console.WriteLine("Name: " + process.Name);
Console.WriteLine("Modules:");
foreach(ProcessModule module in process.Modules) {
Console.WriteLine(module.FileName);
}
Or do like this, 或者这样做
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public static void Main()
{
int chars = 256;
StringBuilder buff = new StringBuilder(chars);
while (true)
{
// Obtain the handle of the active window.
IntPtr handle = GetForegroundWindow();
// Update the controls.
if (GetWindowModuleFileName(handle, buff, chars) > 0)
{
Console.WriteLine(buff.ToString());
Console.WriteLine(handle.ToString());
}
Thread.Sleep(1000);
}
}
From: How to get a process window class name from c#? 来自: 如何从C#获取过程窗口类名称?
int pidToSearch = 316;
//Init a condition indicating that you want to search by process id.
var condition = new PropertyCondition(AutomationElementIdentifiers.ProcessIdProperty,
pidToSearch);
//Find the automation element matching the criteria
AutomationElement element = AutomationElement.RootElement.FindFirst(
TreeScope.Children, condition);
//get the classname
var className = element.Current.ClassName;
I am not sure how you are calling these programs. 我不确定您如何调用这些程序。 If you running these programs through Process, you can get through ProcessName.
如果通过Process运行这些程序,则可以通过ProcessName获得。
Example: 例:
Process tp = Process.Start(@"notepad.exe", "temp");
string s = tp.ProcessName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.