C# – How to programmatically get a list of locked files under a folder structure

c++filewindowswindows-server-2003

Following up from this question: How can I unlock a file that is locked by a process in .NET, how do I programmatically get a list of files that are locked in a particular folder and its subfolders?

I'm using Windows 2003, .NET 3.5, C# 3.0.

Update: some background… basically we're archiving closed websites on a shared server. After deleting the site from IIS we move the folder structure for the site to an archive where it's held for 6 weeks. Even though the IIS site and FTP has been stopped and deleted we still encounter locked files during the move. So it'd be nice to know upfront what's still got a lock on it.

Best Solution

As answered in another question :How to check for file lock?

The only way would be trying them all and add them to the list if it throws an exception. As mentioned in the above question, it would not be a reliable list to use, but only give you an overview of a specific point in time.