To check whether a file is in use or not in .Net program

As the saying goes, Prevention is better than cure. It is always better to check whether file is in use before doing any file operation on it to avoid any unexpected IOException during the runtime.

Exception in question is:

System.IO.IOException: The process cannot access the file 'C:\Inetpub\XXX.jpg' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights,…)
at System.IO.FileStream..ctor(String path, FileMode mode)

Here is simple code...

 

Read More @ Asprangers