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";
        }
        }
    }
leppie
  • 115,091
  • 17
  • 196
  • 297
charlie9495
  • 87
  • 2
  • 17

1 Answers1

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;
    }
}
user3021830
  • 2,784
  • 2
  • 22
  • 43
  • thanks for the reply sir. If you don't mind, where should I place this code? I am quite new to c#. – charlie9495 Feb 05 '15 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. – user3021830 Feb 05 '15 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. – charlie9495 Feb 05 '15 at 13:37
  • Storing the process info in a list and fetching whenever might be the solution then. can you check the updated code? – user3021830 Feb 05 '15 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? – charlie9495 Feb 05 '15 at 13:54
  • It seems OK. Remember that not all applications need to have the same file name with the application title. This is how it seems on my PC. I have matched all running applications with their running processes. http://tinypic.com/view.php?pic=5zpg7m&s=8#.VNN4NGjkfSk – user3021830 Feb 05 '15 at 14:05
  • oh I see, but on my previous code, It would show the actual name of the application and its filename. But thanks for this sir. A thumbs up for you and your effort. This helps me alot. – charlie9495 Feb 05 '15 at 14:36
  • I am glad to be helped. – user3021830 Feb 05 '15 at 14:39
  • Good day sir. May you please explain this part here sir? I am quite confused on how this works. ProcessInfoItem pii = piis.FirstOrDefault(x => x.Id == (int)(sender as ListBox).SelectedValue); if (pii != null) – charlie9495 Oct 11 '15 at 20:45
  • Good day to you too @charlie9495. In the question as I check the codes I see that he is making the selection from a ComboBox. At Form_Load() event we get all processes and load them to ComboBox. Then whenever the user chooses an item from the ComboBox, we retrieve the information for that spesific Process from a set of Process infoes we have previously loaded into memory in GetAllProcessInfos() method. The code you are asking for, checks if there is a matching process with the given Id provided by the ComboBox in this set of Process infos and returns the match if there exists any. – user3021830 Oct 12 '15 at 08:02
  • For further information you can Google for "lambda expressions C#" They are a shorthand notation to delegate certain methods. – user3021830 Oct 12 '15 at 08:02