C#列出当前打开的文件和程序 [英] C# List currently open files and programs

查看:107
本文介绍了C#列出当前打开的文件和程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以得到所有打开的应用程序列表和所有打开的文件列表?对于文件,我只需要打开的文件(文件等),而不是操作系统的开放系统文件。同样的应用程序(只有浏览器,文档处理器等)。

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

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

p>

Microsoft Word,
记事本,
Mozilla Firefox

文件



foo.txt,
foo.mp3,
foo.doc



我需要的只是名称,我不需要处理等(即使我确信我将不得不使用它们来得到我想要的)。 解决方案

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

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

//列出所有进程并将它们写入到一个stringbuilder中
ManagementClass MgmtClass = new ManagementClass(Win32_Process);
$ b $ foreach(MgmtClass.GetInstances()中的ManagementObject mo)
{
sb.Append(Name:\ t+ mo [Name] + Environment.NewLine );
sb.Append(ID:\ t+ mo [ProcessId] + Environment.NewLine);
sb.Append(Environment.NewLine);
}
return sb.ToString();





$ b

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

  //您将需要引用System.Management.Dll并使用System .Management命名空间
public string GetProcessOwner(string processName)
{
string query =Select * from Win32_Process Where Name = \+ processName +\;

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

foreach(processList中的ManagementObject obj)
{
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] ;
回报所有者;
}
}

返回NO OWNER;

$ / code>

对于打开的文件列表,可以使用Managed Code是不知何故的。这是一个 example on codeproject demonstration that same matter

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)

解决方案

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

这篇关于C#列出当前打开的文件和程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆