简体   繁体   English

如何获取使用C#在Windows上运行的程序的名称

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

相关问题 如何在C#中获取Windows登录名 - how to get windows login name in c# 如何在C#中知道程序在运行XP SP2的Mac上的Parallels上运行,还是在运行Windows 7的Mac上的VmWare上运行 - How to know in C# that program is running on Parallels on a Mac running XP SP2 or VmWare on a Mac running Windows 7 如何使用C#Windows Service获取正在运行的应用程序 - How to Get Running Application with C# Windows Service 如何使用 C# 获取运行代码的操作系统的名称? - How to get the name of OS in which the code is running using C#? 在C#中我如何获取正在运行的文件的名称? - In C# how do i get the name of the running file? 如何在C#Web APP中获取当前的Windows登录名? - How to get current Windows Login Name in C# Web APP? 如何在Windows / C#上获取顶部窗口的进程名称和标题 - How to get process name and title of the top window on Windows / C# 如何在C#中使用Windows身份验证获取SQL Server名称 - How to get SQL Server Name Using Windows Authetication in C# 如何获取c# windows应用程序中颜色的颜色名称字符串 - How to get the Color Name String of Color in c# windows application C#:在运行程序并使用 if 语句回答输入后,如何让程序继续提问? - C#: After running program and the input is answered with an if statement, how do i get the program to keep asking the question?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM