English 中文(简体)
C# List currently open files and programs
原标题:

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:	" + mo["Name"] + Environment.NewLine);
        sb.Append("ID:	" + 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 DOMAINuser
                    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

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签