简体   繁体   中英

Check if folder is in use in C#

Consider a network folder:

\\\\desiis\\c$\\Company\\B2b\\Monitor

On that machine, any process that tries to delete the directory Monitor receives an error because a user on the LAN has that directory open (likely with Windows Explorer).

Can I detect, via C# and .NET framework, if any user (and which user) has a particular directory open/in use?

No, there is no possibility I know.

It seems you'll have to catch the occuring Exception as a workaround.

I'm not sure if you can obtain the particular user in the directory but the DirectoryInfo class in C# .NET would probably be the best way to go.

http://msdn.microsoft.com/en-us/library/system.io.directoryinfo_members.aspx

Review the API at this link and you'll notice there is a method to get the information about WHEN the directory was last accessed but not by who. Also you can catch any exception when trying to delete a directory as it being unavailable (an exception will be thrown as you are probably well aware).

Also note that exception catching is costly and you should evaluate any slowdowns in your process by doing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM