[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

c# Find all processes and DLLs that currently running, from a specific folder

I'm writing something to automate a software release to 800 servers.  The current process is very manual.  Most of the automation is quite simple, but one of the requirements, is that before the release takes place, any files that are in use from the folder must be freed up.  In order to do this, I need to find any running processes that originate from the folder (fairly simple), but I also need to find any processes (located anywhere on the server) that are using DLLs from the folder, as there may be other programs on the machine that are using DLLs from my target folder.
Any ideas on how I can get a list of DLLs in use by processes, and then check their path?


1 Solution
Hello lambch0p,

Take a look:

How do I find out which process is locking a file using .NET?


lambch0pAuthor Commented:
Thanks Rimvis.

I came up with the code below, which is a lot simpler, but all it does is list processes and their loaded modules, if either the process or module comes from a particular folder.

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

namespace test
    class Program
        static void Main(string[] args)
            //Get all the processes
            Process[] processlist = Process.GetProcesses();

            foreach (Process theprocess in processlist)
                //ignore system and idle, as they seem to break.
                //could handle this properly, but that's not for now
                if (theprocess.ProcessName.ToUpper() != "SYSTEM" && theprocess.ProcessName.ToUpper() != "IDLE")
                    //spit out the processname
                    //get a list of modules used by the process
                    ProcessModuleCollection pmc = theprocess.Modules;
                    foreach (ProcessModule pm in pmc)
                        //if the module filename contains the path i'm interested in
                        //write it out (or do something more useful)
                        if (pm.FileName.ToUpper().Contains("C:\\MYFOLDER"))
                            Console.WriteLine("\t" + pm.FileName);

Open in new window


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now