如何获取当前正在运行的进程的文件名?

时间:2014-06-01 06:34:59

标签: c# winforms process

这就是我获得所有工作流程的方式:

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

这就是我在目录中获取文件的方式:

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

我想获取每个进程目录中的文件。 这是我使用的方法:

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();
}

但是我没有获取C:\中只有文件的图标,而是想获取当前正在运行的进程的图标。

1 个答案:

答案 0 :(得分:2)

基于this

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

    ...
}