1

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)

5
  • The files are opened by the programs. You aren't opening them. You need to ask the programs what they are using. So you need to either read their API, or if you're lucky they may have it in the title Commented Jan 21, 2011 at 10:22
  • Yes but the programs ask the OS for the files to open. So, the OS probably know what files are in use and by which program, right? Commented Jan 21, 2011 at 10:23
  • 1
    Have you tried .Net Process List? (System.Diagnostics) Process[] processList = Process.GetProcesses(); Commented Jan 21, 2011 at 10:25
  • @RoBYCoNTe +1 same as my reply, the simplest way to go. Commented Jan 21, 2011 at 10:27
  • Scusami Felice, quando scrivevo il tuo commento non era ancora visibile! :) (Italiano?) Commented Jan 21, 2011 at 10:37

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is most simple using System.Diagnostic.Process.GetProcesses()
The suggested code works but returns all executables that are currently running including programs that run in the background or system programs. Can't I just get a list of the programs that the user started?
@user, The only way I know is to get the process owner and check if it's "System" then it's not started by user. I updated the answer with the code to check the process Owner.
Thanks for your help. I think I'm getting somewhere.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.