

In your code you are opening the file, closing it, and then re-opening it. Once you close the file it is no longer locked. The file will only be locked as long as you have it open. To prevent a file from being written to (to "lock" it) you must open it with FileShare.Read (which will still allow others to read it) or FileShare.None (which will not allow others to read or write it). So to check "whether the file is currently using outside the application" you need to try to open the file with a FileStream (with whatever FileAccess you need), and catch any exception that occurs. The same thing for reading-try to open the file with FileAccess.Read. If it is locked for writing, you will get an Exception, which you must handle as appropriate for your application. How do you check if a file is locked for writing? By trying to open it with FileAccess.Write. Consider this: the only thread-safe way to check if a file is locked is to try and open it with the access you require-you must treat locking a file and opening a file as the same thing. I still don't understand your requirement, but it should be quite simple to meet. Read) Also I want to check whether the file is currently using outside the application or not. GetCurrent().Name.ToString().Split( new char ) + "\r\r\n\r\n" ) File. Sw.Write( StreamWriter sw = new StreamWriter (fstreamLock) I am using the following code to create a lock file
