Home > Diagnostics, WPF > Iterates through process and their libraries

Iterates through process and their libraries

Following WPF application uses System.Diagnostics namespace to get the list of all running processes. Then for each process it get the list of attached modules and show the result in a TreeView control.

Following XAML code build the content of the window. Nothing fancy.

<Window x:Class=”Processes.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
    Title=”Window1″ Height=”300″ Width=”300″>
    <DockPanel LastChildFill=”True” >
        <Button DockPanel.Dock=”Top” Name=”Button1″ Click=”Button1_Click”>
            Get All Processes
        </Button>
        <TreeView DockPanel.Dock=”Bottom” Name=”TreeView1″ >
        </TreeView>
    </DockPanel>   
</Window>

 

And the code behind is:

 

public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
  }

  private void Button1_Click(object sender, RoutedEventArgs e)
  {
    foreach (Process process in Process.GetProcesses())
    {
      TreeViewItem head = new TreeViewItem();
      head.Header = process.ProcessName;
      TreeViewItem files = new TreeViewItem();
      files.Header = “Modules”;
      try
      {
        foreach (ProcessModule module in process.Modules)
        {
          files.Items.Add(new TreeViewItem
          {
            Header = module.ModuleName + “:”
              + module.FileName + “(”
              + module.FileVersionInfo.FileVersion + “)”
          });
        }
      }
      catch (Exception)
      {
        files.Items.Add(new TreeViewItem { Header = “Access denied” });
      }

      head.Items.Add(files); 
      TreeView1.Items.Add(head);    
    }
  }
}

 

The running result would be a simple window with only one button. Pressing the button loads the list of all process into the treeview:

image

 
To project is created in VS 2008 Professional Edition: Download Souce Code

Advertisements
Categories: Diagnostics, WPF
  1. April 16, 2008 at 9:37 pm

    Take a look at the April 8th post at: http://blog.quantumbitdesigns.com/category/wpf/ (permalink is broken for that post)…

    Similar concept, different data.
    Thanks for blogging about WPF!
    -Rob

    Rob Relyea | WPF & Xaml Language Team
    robrelyea.com | /blog | /wpf | /xaml

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: