Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Friday, March 09, 2007

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] -
.NET | Fun Stuff
Comments are closed.
On this page
Archive
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

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

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 260
This Year: 57
This Month: 0
This Week: 0
Comments: 116
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)