简体   繁体   中英

How can I get the file name of currently running process?

This is how I'm getting all the working processes:

Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
      listBox1.Items.Add(theprocess.ProcessName);
}

And this is how I get the files in a directory:

foreach (System.IO.FileInfo file in dir.GetFiles())
{
}

I want to get the files in directory of each process. This is the method i'm using:

ListView listView1;
ImageList imageList1;

public void ExtractAssociatedIconEx()
{
    listView1 = new ListView();
    imageList1 = new ImageList();
    listView1.Location = new Point(37, 12);
    listView1.Size = new Size(151, 262);
    listView1.SmallImageList = imageList1;
    listView1.View = View.SmallIcon;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.listView1);
    this.Text = "Form1";

    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\");

    ListViewItem item;
    listView1.BeginUpdate();


    foreach (System.IO.FileInfo file in dir.GetFiles())
    {

        Icon iconForFile = SystemIcons.WinLogo;

        item = new ListViewItem(file.Name, 1);
        iconForFile = Icon.ExtractAssociatedIcon(file.FullName);

        if (!imageList1.Images.ContainsKey(file.Extension))
        {           
            iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName);
            imageList1.Images.Add(file.Extension, iconForFile);
        }

        item.ImageKey = file.Extension;
        listView1.Items.Add(item);
    }
    listView1.EndUpdate();
}

But instead of getting the icons of only the files in C:\\, I want to get the icons of the current running processes.

Based on this :

foreach (Process in Process.GetProcesses())
{
    string filePath = process.Modules[0].FileName;
    Icon fileIcon = Icon.ExtractAssociatedIcon(filePath);

    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM