C# 如何获取Process.MainMoudle.FileName?

C# 如何获取Process.MainMoudle.FileName?,c#,.net,winforms,C#,.net,Winforms,我有这门课: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using System.ComponentModel; namespace HardwareMonitoring { public class GetProcesse

我有这门课:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;

namespace HardwareMonitoring
{


    public class GetProcesses: IWin32Window

    {
        internal IntPtr handle;
        internal String title;
        internal String filename;

        public IntPtr Handle
        {
            get { return handle; }
        }

        public String Title
        {
            get { return title; }
        }

        public String FileName
        {
            get { return filename; }
        }

        public static readonly Int32 GWL_STYLE = -16;
        public static readonly UInt64 WS_VISIBLE = 0x10000000L;
        public static readonly UInt64 WS_BORDER = 0x00800000L;
        public static readonly UInt64 DESIRED_WS = WS_BORDER | WS_VISIBLE;

        public delegate Boolean EnumWindowsCallback(IntPtr hwnd, Int32 lParam);

        public static List<GetProcesses> GetAllWindows()
        {
            List<GetProcesses> windows = new List<GetProcesses>();
            StringBuilder buffer = new StringBuilder(100);
            EnumWindows(delegate(IntPtr hwnd, Int32 lParam)
            {
                if ((GetWindowLongA(hwnd, GWL_STYLE) & DESIRED_WS) == DESIRED_WS)
                {
                    GetWindowText(hwnd, buffer, buffer.Capacity);
                    GetProcesses wnd = new GetProcesses();
                    wnd.handle = hwnd;
                    wnd.title = buffer.ToString();
                    windows.Add(wnd);
                }
                return true;
            }, 0);

            return windows;
        }

        [DllImport("user32.dll")]
        static extern Int32 EnumWindows(EnumWindowsCallback lpEnumFunc, Int32 lParam);

        [DllImport("user32.dll")]
        public static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);

        [DllImport("user32.dll")]
        static extern UInt64 GetWindowLongA(IntPtr hWnd, Int32 nIndex);
    }
}
使用wnd,我可以获取进程的句柄和标题,但我还需要获取文件名

如果我在做:

foreach (Process p in Process.GetProcesses()) 
然后我可以做:

p.MainModule.FileName
如何在类中使用获取FileName属性?我在那里添加了属性,但如何获得它的值

编辑

在课程底部,我添加了:

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
然后在课堂上我用了它:

wnd.processid = GetWindowThreadProcessId(wnd.handle, IntPtr.Zero);
表格1:

foreach (var wnd in GetProcesses.GetAllWindows())
            {

            }
    string ff = "";
    void PopulateApplications()
    {

        foreach (var wnd in GetProcesses.GetAllWindows())
        {
            uint uuu = wnd.ProcessId;
            ff = Process.GetProcessById((int)uuu).MainModule.FileName;
        }
    }
但当它在排队时:

ff = Process.GetProcessById((int)uuu).MainModule.FileName;
它只会退出程序。没有错误或例外

编辑

在班级底部:

[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
然后在表格1中使用它:

    int ff = 0;
    List<string> FileNames = new List<string>();
    void PopulateApplications()
    {

        foreach (var wnd in GetProcesses.GetAllWindows())
        {
            GetProcesses.GetWindowThreadProcessId(wnd.handle, out ff);
            FileNames.Add(Process.GetProcessById(ff).MainModule.FileName);
        }
    }
int ff=0;
列表文件名=新列表();
void PopulateApplications()
{
foreach(GetProcesses.GetAllWindows()中的变量wnd)
{
GetProcesses.GetWindowThreadProcessId(wnd.handle,out ff);
添加(Process.GetProcessById(ff.MainModule.FileName);
}
}

我只得到18个进程文件名。

Process.GetProcesses()返回的进程太多。您还将获得在使用MainModule属性时引发异常的异常。与系统进程一样,它不是一个真正的进程,也没有主模块。只是不要使用它,EnumWindows()已经为您提供了所需的内容。Pinvoke GetWindowThreadProcessId()获取拥有该窗口的进程的进程ID。Process.GetProcessById()下一步。如果您试图重新发明任务管理器,请记住它运行时是提升的。Hans我不打算使用GetProcess,但当我使用它时,我可以这样做:var icon=icon.ExtractAssociatedIcon(p.MainModule.FileName);因此,我想知道现在在使用EnumWindows()时如何才能做到这一点。我解释了如何使用Process.GetProcessById()。尝试一下。尝试一下,我用我所做的编辑了我的问题,我在变量uuu的form1中看到id号改变了每个循环,但当它到达行文件名时,它只是退出程序,什么也不做。您没有正确使用GetWindowThreadProcessId()。它返回拥有窗口的线程的线程ID。你需要使用第二个参数。声明错误,第二个参数必须是
out int pid
。需要进行错误检查。