Get All Modules Used by Process in C#

In this article you will get a list of all modules used by a Process in C#.

Introduction

A process may use one or more modules. A module is either an .exe or a .dll file. By using the windows Task Manager, you can see a list of all the processes that are currently running on the computer on the "Process" tab. However, have you ever wondered where the .exe or .dll files displayed on the Process tab are located? Sometimes you may suspect that an application is using .dll files from the wrong location. How can you determine that? The "Process" class provides the Modules method that you can use to retrieve a list of all the modules used by a process. By using this method, you can also retrieve information about a specific module, such as the name, the file name, and the memory used by the module. This information can help evaluate and optimize the performance of the application.

The following code sample shows the use of the "Modules" method. In this code sample, you create a process called myProcess. You set its "StartInfor" property so that it executes Notepad. You then display the modules that are associated with Notepad and output information about each module to the command console. Then you display the same information for Notepad.

Code

class ProcessModules
    {
        public void getModules()
        {
            Process myProcess = new Process();
            //Get the process start information of notepad
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
            //Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
            myProcess.StartInfo = myProcessStartInfo;

            //Create a notepad
            myProcess.Start();
            System.Threading.Thread.Sleep(1000);
            ProcessModule myProcessModule;
            //Get all the modules associated with 'myProcess".
            ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
            Console.WriteLine("Properties of the modules associated with 'notepad' are:");
            //Display the properties of each of the modules
            for (int i = 0; i < myProcessModuleCollection.Count; i++)
            {
                myProcessModule = myProcessModuleCollection[i];
                Console.WriteLine("The moduleName is " + myProcessModule.ModuleName);
                Console.WriteLine("The " + myProcessModule.ModuleName + "'s File Name is: " + myProcessModule.FileName);
                Console.WriteLine("The " + myProcessModule.ModuleName + "'s base address is: " + myProcessModule.BaseAddress);
                Console.WriteLine("For " + myProcessModule.ModuleName + " Entry point address is: " + myProcessModule.EntryPointAddress);
            }
            //Get the Main module associate with 'myProcess'
            myProcessModule = myProcess.MainModule;
            Console.WriteLine("The Main Module associated");
            Console.WriteLine("The process's main modulename is " + myProcessModule.ModuleName);
            Console.WriteLine("The process's main modulename  File Name is: " + myProcessModule.FileName);
            Console.WriteLine("The process's main modulename base address is: " + myProcessModule.BaseAddress);
            Console.WriteLine("The process's main modulename Entry point address is: " + myProcessModule.EntryPointAddress);
            myProcess.CloseMainWindow();

        }
    }