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?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello lambch0p,

Take a look:

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



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.