C# 如何用c获取windows上运行的程序名#

C# 如何用c获取windows上运行的程序名#,c#,process,C#,Process,我想要一个函数,给我一个程序名(如msPaint或记事本),当我使用记事本时,该函数返回“记事本”,当我使用msPaint时,该函数返回“msPaint” 请帮助我…使用Process.getprocesss()method您可以获取所有正在运行的应用程序,如果需要当前活动窗口,请使用getforegroundindow()和GetWindowText() 例如来自: 您可以使用Process类。它有一个Modules属性,列出所有加载的模块 要在控制台中列出所有进程和所有模块,请执行以下操作:

我想要一个函数,给我一个程序名(如msPaint或记事本),当我使用记事本时,该函数返回“记事本”,当我使用msPaint时,该函数返回“msPaint”


请帮助我…

使用
Process.getprocesss()
method您可以获取所有正在运行的应用程序,如果需要当前活动窗口,请使用
getforegroundindow()
GetWindowText()

例如

来自:


您可以使用Process类。它有一个Modules属性,列出所有加载的模块

要在控制台中列出所有进程和所有模块,请执行以下操作:

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);
    }
或者像这样做,

[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);
    }
}

我不知道你怎么称呼这些程序。若您通过Process运行这些程序,那个么就可以通过ProcessName

例如:

        Process tp = Process.Start(@"notepad.exe", "temp");

        string s = tp.ProcessName;

如何将“GetForegroundWindow()”更改为此程序的名称?这可能会有所帮助
        Process tp = Process.Start(@"notepad.exe", "temp");

        string s = tp.ProcessName;