C# 列出当前打开的文件和程序

发布于 2024-10-13 01:33:00 字数 337 浏览 6 评论 0原文

有没有办法获取所有打开的应用程序的列表和所有打开的文件的列表?对于文件,我只需要我打开的文件(文档等),而不是操作系统的打开系统文件。应用程序也是如此(仅浏览器、文档处理器等)。

我已经尝试了 Windows API 中的各种功能,例如 EnumWindows,但我无法得到我想要的。

我的最终目标的一个例子是拥有这样的列表:

应用程序

Microsoft Word, 记事本, Mozilla Firefox

文件

foo.txt, foo.mp3, foo.doc

我需要的只是名称,我不需要句柄等(尽管我确信我必须使用它们来获得我想要的东西)

Is there a way I can get a list of all open applications and a list of all open files? For the files I only need the files that I opened (documents etc) not OS's open system files. The same for the applications (only browsers, document processors etc).

I already tried various functions from the Windows API like EnumWindows but I couldn't get what I wanted.

An example of what my ultimate goal would be, is to have lists like this:

Applications

Microsoft Word,
Notepad,
Mozilla Firefox

Files

foo.txt,
foo.mp3,
foo.doc

What I need is just the names, I don't need handles etc (even though I'm sure I'll have to use them to get what I want)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不打扰别人 2024-10-20 01:33:00

您可以获得正在运行的进程及其信息的列表。

public static string ListAllProcesses()
{
    StringBuilder sb = new StringBuilder();

    // list out all processes and write them into a stringbuilder
    ManagementClass MgmtClass = new ManagementClass("Win32_Process");

    foreach (ManagementObject mo in MgmtClass.GetInstances())
    {
        sb.Append("Name:\t" + mo["Name"] + Environment.NewLine);
        sb.Append("ID:\t" + mo["ProcessId"] + Environment.NewLine);
        sb.Append(Environment.NewLine);
    }
    return sb.ToString();
}

查看进程是否由用户或系统打开的唯一方法(据我所知)是检查其所有者。如果它是系统,那么它不是由用户运行:

//You will need to reference System.Management.Dll and use System.Management namespace
public string GetProcessOwner(string processName)
        {
            string query = "Select * from Win32_Process Where Name = \"" + processName + "\"";

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            ManagementObjectCollection processList = searcher.Get();

            foreach (ManagementObject obj in processList)
            {
                string[] argList = new string[] { string.Empty, string.Empty };
                int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
                if (returnVal == 0)
                {
                    // return DOMAIN\user
                    string owner = argList[1] + "\\" + argList[0];
                    return owner;
                }
            }

            return "NO OWNER";
        }

对于打开的文件列表,可以使用托管代码来完成,但这在某种程度上是困难的。这是 codeproject 上的示例 演示了相同的问题

You can get a list of running processes with their information

public static string ListAllProcesses()
{
    StringBuilder sb = new StringBuilder();

    // list out all processes and write them into a stringbuilder
    ManagementClass MgmtClass = new ManagementClass("Win32_Process");

    foreach (ManagementObject mo in MgmtClass.GetInstances())
    {
        sb.Append("Name:\t" + mo["Name"] + Environment.NewLine);
        sb.Append("ID:\t" + mo["ProcessId"] + Environment.NewLine);
        sb.Append(Environment.NewLine);
    }
    return sb.ToString();
}

The only method (That I know) to see if the process is opened by user or system is to check it's owner. If it's system, then it's not run by user:

//You will need to reference System.Management.Dll and use System.Management namespace
public string GetProcessOwner(string processName)
        {
            string query = "Select * from Win32_Process Where Name = \"" + processName + "\"";

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            ManagementObjectCollection processList = searcher.Get();

            foreach (ManagementObject obj in processList)
            {
                string[] argList = new string[] { string.Empty, string.Empty };
                int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
                if (returnVal == 0)
                {
                    // return DOMAIN\user
                    string owner = argList[1] + "\\" + argList[0];
                    return owner;
                }
            }

            return "NO OWNER";
        }

For the list of opened files, It is possible to do using Managed Code which is somehow hard. Here is an example on codeproject demonstrating the same matter

深海夜未眠 2024-10-20 01:33:00

您可以使用 Process.GetProcesses() 获得正在运行的进程列表: http:// msdn.microsoft.com/en-us/library/1f3ys1f9.aspx
但是,如果他们公开了您知道的一些自动化接口,您就可以让他们打开文件。

You can have a list of running processes with Process.GetProcesses(): http://msdn.microsoft.com/en-us/library/1f3ys1f9.aspx
But you can have the file they have open if they exposes some automation interface you know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文