6

I am trying to determine whether or not a file is currently open or being written to using C#. I've seen similar SO questions, all with a similar solution as my code, which attempts a File.Open on a file. But when I run the program with the code below, and I also manually have the file open, I get the unexpected result of "File is currently NOT locked". Any thoughts/suggestions/anything I'm missing?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace TestIfFileAccessed { class Program { static void Main(string[] args) { string path = @"C:\TEMP\testFile.txt"; FileInfo filepath = new FileInfo(path); if (IsFileLocked(filepath)) { Console.WriteLine("File is currently locked"); Console.ReadLine(); } else { Console.WriteLine("File is currently NOT locked"); Console.ReadLine(); } } public static bool IsFileLocked(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; } } } 
13
  • At least put a using(...) statement on that file.Open(). Commented Aug 6, 2015 at 19:23
  • 1
    Are you sure you're file is locked when you manually open it? Commented Aug 6, 2015 at 19:23
  • 1
    @whoisj What do you think stream.Dispose() does that stream.Close() doesn't already do? Commented Aug 6, 2015 at 19:24
  • 1
    Perhaps you're opening the file in a program that reads the file into memory. Maybe this will work if you open it in another program using FileInfo.Open and don't close it. Give it a try :) Commented Aug 6, 2015 at 19:24
  • 2
    Hard to guess what "also manually have the file open" might mean. Well, not what you think it does. Lots of programs that read .txt files just read its content and close the file again. Like Notepad or VS. Commented Aug 6, 2015 at 19:24

2 Answers 2

9

Text files aren't usually locked - their info is usually pulled and loaded into memory, so unless you were trying to access it at the exact same moment that another program is loading the file (which would go very quickly) then you shouldn't have any problems.

Source - Similar question

EDIT: If it is open in word then you will have problems, as Word keeps the stream open. Try opening the file in Word (if you have it) and running your code again, I believe it should work. Otherwise, if you want to see if it's open in Notepad, you would have to scan the current processes running on the system for notepad and check if the process opened the file.

Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately the OP cannot rely on the usual behavior.
Are you sure ? please add a reference for your answer.
@BehzadKhosravifar This answer is entirely correct and is easily verifiable by anyone including yourself. Just try opening the same file in Notepad twice.
Upvoted. This answer is 100% correct. I just wanted to elaborate in my answer. Nicholas is absolutely right though.
Elaboration is always welcome, +1 to yours as well :) The more we know, the better.
|
3

Many text editors don't actually put a file lock on the file. For instance, I use Sublime Text 3 to edit files. I can open an .txt file in Sublime, and open the SAME file in another editor, such as notepad. There is not a filelock on the .txt file.

What is happening is that the file is loaded into memory by the editors when you open them and then closed immediately. They aren't left open. Editors only keep the files open long enough to load them, however, there technically is a lock on the file while the editor is is loading the file into RAM. It just doesn't last very long as this operation is very quick.

You could cause a file lock and see your filelock error as expected if you do filepath.Open(); before you call IsFileLocked(filepath). I would suggest trying this code in your Main method if you want to test this out:

static void Main(string[] args) { string path = @"C:\TEMP\testFile.txt"; FileInfo filepath = new FileInfo(path); Console.WriteLine("Opening \"filepath\""); filepath.Open(); FileInfo filepath2 = new FileInfo(path); if (IsFileLocked(filepath2)) { Console.WriteLine("File is currently locked"); } else { Console.WriteLine("File is currently NOT locked"); } Console.WriteLine("Closing \"filepath\""); filepath.Close(); if (IsFileLocked(filepath2)) { Console.WriteLine("File is currently locked"); } else { Console.WriteLine("File is currently NOT locked"); } } 

I hope this helps to clarify what is going on.

I vow to fall on my sword if this answer turns out to be incorrect and dishonors my family.

10 Comments

thanks. I am seeing now that just opening a file with an application does not necessarily lock it. So if a file is being written to, it is considered "open" and "locked" during that time? I see that this simulates an application opening the file, but I want to confirm that a file being written to is also consider locked.
Yes, if a file is being written to then it is locked. However, for a text file, the writing would be very quick. If you want to simulate opening the file, just open it in word, as word keeps the stream open.
Yes, a file has to be open and locked to be written to or read from. These operations are super quick and most editors close the files immediately after the operation finishes and uses RAM as it's workspace. Nicholas is correct. If an editor (in this case, Word) doesn't close the file after loading it, the file will remain locked. even thought it's not actively being read from or written to.
No, different applications handle opening text files differently. So as I was saying, the most common way to open a text file is to lock it, read all of it, and then unlock it right away. Word (and some other programs) handle it by locking it and then keeping it locked the entire time it's open.
To simulate it (if you want to do so using code) simply make a new project, open the text file using a var reader = new StreamReader(fileLocation) and then loop forever using a while(true) loop. The file is opened in your reader stream and never closed, making it locked.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.