0

Good day!

I really need your help. I am trying to get the path of the running applications that the program detects but whenever I click a particular name of a running app, it will only returns the path of the project (the windows application project that I am coding in). I am confused on how to solve this problem.

Here are my codes:

    namespace getting_apps
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.ValueMember != "")
            {
                textBox1.Text = listBox1.SelectedValue.ToString();                
                string path;
                path = System.IO.Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                textBox2.Text = path;

            }
            if (textBox2.Text == "getting_apps")
            {
                MessageBox.Show("asldfjasdklfjasdf");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ProcessName");
            dt.Columns.Add("ProcessID");
            foreach (Process p in Process.GetProcesses("."))
            {
                try
                {
                    if (p.MainWindowTitle.Length > 0)
                    {
                        dt.Rows.Add();
                        dt.Rows[dt.Rows.Count - 1][0] = p.MainWindowTitle;
                        dt.Rows[dt.Rows.Count - 1][1] = p.Id.ToString();

                    }
                }
                catch { }
            }
            listBox1.DataSource = dt;
            listBox1.DisplayMember = "ProcessName";
            listBox1.ValueMember = "ProcessId";
        }
        }
    }

1 Answer 1

4

Use the following code to get neccessary information :

        Process currentProcess = Process.GetCurrentProcess();
        Process[] localAll = Process.GetProcesses();
        foreach (Process p in localAll)
        {
            if (p.MainModule != null)
            {
                int processId = p.Id;
                string name = p.MainModule.ModuleName;
                string filename = p.MainModule.FileName;
            }
        }

But, if the application will run on a 64-bit OS change "Platform Target" to "x64" and uncheck "Prefer 32-bit" from Properties/Build window.

Full code becomes:

private List<ProcessInfoItem> piis = new List<ProcessInfoItem>();

private void Form1_Load(object sender, EventArgs e)
{
    piis = GetAllProcessInfos();
    listBox1.DisplayMember = "Name";
    listBox1.ValueMember = "Id";
    listBox1.DataSource = piis;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ProcessInfoItem pii = piis.FirstOrDefault(x => x.Id == (int)(sender as ListBox).SelectedValue);
    if (pii != null)
    {
        MessageBox.Show(pii.FileName);
    }
}
private List<ProcessInfoItem> GetAllProcessInfos()
{
    List<ProcessInfoItem> result = new List<ProcessInfoItem>();
    Process currentProcess = Process.GetCurrentProcess();
    Process[] localAll = Process.GetProcesses();
    foreach (Process p in localAll)
    {
        try
        {
            if (p.Id != 4 && p.Id != 0 && p.MainModule != null)
            {
                ProcessInfoItem pii = new ProcessInfoItem(p.Id, p.MainModule.ModuleName, p.MainModule.FileName);
                result.Add(pii);
            }
        }
        catch (Win32Exception)
        {   // Omit "Access is denied" Exception
        }
        catch (Exception)
        {
            throw;
        }
    }

    return result;
}
public class ProcessInfoItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string FileName { get; set; }
    public ProcessInfoItem()
    { }
    public ProcessInfoItem(int id, string name, string filename)
    {
        this.Id = id;
        this.Name = name;
        this.FileName = filename;
    }
}
11
  • thanks for the reply sir. If you don't mind, where should I place this code? I am quite new to c#. Commented Feb 5, 2015 at 12:52
  • Note that this is a tricky solution. I did not want to mess with unmanaged code, so I have just ommitted access rights and intentionally neglected processes with Ids 0 and 4, Idle and System respectively, which you can not access at all. Commented Feb 5, 2015 at 13:20
  • 1
    Not to be rude or anything but I think were not on the same page or it's just that I just don't get it but anyways, I already got the running applications and it's process ID but the problem is that whenever I click a certain application listed, it would return the path on the project that I am working on. Commented Feb 5, 2015 at 13:37
  • Storing the process info in a list and fetching whenever might be the solution then. can you check the updated code? Commented Feb 5, 2015 at 13:45
  • Thanks for the followup sir. It is working now but is that I can't see the applications that I have open like stickynotes, notepad and the like. It seems like I can only see applications which are working on the background? Commented Feb 5, 2015 at 13:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.