How To: Check if there are processes that are running using .NET#

I was running a test on my Web Server today when a friend of mine IMed me about an interesting question about .NET. He was a new programmer for this company and the old developer that he replaced was running production(oh my) applications right of his desktop. The IT manager was getting worried because they dont have a list of applications that the old guy did because the old developer didnt do any turnover(he was fired =)) and alot of the apps that he did were essential to the business(the company was a small accounting firm). So my friend asked me if theres a way to figure out which applications are running using managed code so that they could check the application and move it to a more secure and dependable server.

I know i could do this using a console command

   c:\ tasklist /fi "modules eq mscoree.dll"

But they wanted more information about the file so i decided build him a small console app. The code below checks to see if there are any process that is using Managed Code and displays what .NET version it is using.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;

namespace KeithRull.WhosUsingDotNet
{
   /// <summary>
   /// A code snippet that checks what applications are running .NET
   /// and tells what version they are using.
   /// </summary>
   class Program
   {
      /// <summary>
      /// Microsoft .NET Runtime Common Language Runtime
      /// </summary>
      const string STR_Mscorwksdll = "mscorwks.dll";

      public static void Main()
      {
         // Create the headers
         Console.WriteLine("{0,-40} {1}", "Process Name", ".NET version");
         Console.WriteLine("{0}", "--------------------------------------------------------");

         // Get a list of running processes
         System.Diagnostics.Process[] runningProcesses = Process.GetProcesses();

         // Initialize a counter
         int manageApplications = 0;

         // Iterate on all running processes
         foreach (Process process in runningProcesses)
         {
            try
            {
               // iterate thru each module in the process
               foreach (ProcessModule processModule in process.Modules)
               {
                  // Get the name of the module
                  string processModuleName = processModule.ModuleName.ToLower();

                  // Check if the process is using the .NET Runtime Library
                  if (processModuleName.Equals(STR_Mscorwksdll))
                  {
                     // Get the name of the process
                     string processName = process.ProcessName;

                     // Get the .NET file version
                     string fileVersion = processModule.FileVersionInfo.ProductVersion;

                     // Print the result to the console
                     Console.WriteLine("{0,-40} {1}", processName, fileVersion);

                     // Add 1 to our counter
                     manageApplications += 1;
                  }
               }
            }
            catch (Win32Exception wex)
            {
               // Exceptions? eh, nothing to do here.
            }
         }

         // Display a report
         Console.WriteLine();
         Console.WriteLine("{0}", "--------------------------------------------------------");
         Console.WriteLine("Processes Found: {0} | Process Running .NET: {1}", runningProcesses.Length, manageApplications);
         Console.WriteLine("{0}", "--------------------------------------------------------");

         //Pause
         Console.ReadLine();
      }
   }
}

Here's a screenshot

Problem Solved! =)

Friday, March 09, 2007 11:54:03 PM (GMT Standard Time, UTC+00:00) #    Comments [0]  | 

 

Comments are closed.
All content © 2010, Keith Rull
On this page
This site
Calendar
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.9074.18820

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: