Check if text file is open in notepad

how to find out if a particular .txt file is open in notepad?

I tried the solutions mentioned here

Is there a way to check if a file is being used?

But they work fine for Word and pdf files, but they do not work for a txt file opened in Notepad.

here is the code i wrote.

public bool IsFileOpen(string strFileName) { bool retVal = false; try { if (File.Exists(pstrFileName)) { using (FileStream stream = File.OpenWrite(pstrFileName)) { try { } catch (IOException) { retVal = true; } finally { stream.Close(); stream.Dispose(); } } } } catch (IOException) { //file is opened at another location retVal = true; } catch (UnauthorizedAccessException) { //Bypass this exception since this is due to the file is being set to read-only } return retVal; } 

I am missing something here. ??

My requirement: I have an application that works like VSS. When the user checks a specific file and opens, and try to check it while it was open. The app should throw a warning message. For this, I used the above functions. It works great for Word and PDF.

+2
source share
3 answers

Expand my comment. The file is locked if the handle remains open by the application. Word, for example, will open the file, read it in the stream, and save the handle so that other applications cannot delete this file while the user is working on it.

Notepad and other applications just open the file, read the entire stream, and close the file that releases the lock they have. This means that the file is no longer locked and can be edited by another application or even deleted, and Notepad does not take care that it has its own copy in memory.

You can try and hack using Notepad instances and check if the file is open, but ultimately this is not a great idea. If the file is not locked, you should be free to do whatever you want with it.

+10
source

This is the hacking solution I just came up with, but it should work for you. It uses System.Diagnostics.

 Process[] processes = Process.GetProcessesByName("notepad"); for (int i = 0; i < processes.Length; i++) { Console.WriteLine(processes[i].MainWindowTitle); if (processes[i].MainWindowTitle.Equals("myFile.txt - Notepad")) { Console.WriteLine("The file myFile is Open!"); } } Console.ReadLine(); 

Hope this should have done the trick. My example shows if a notepad instance is open with the window title "myFile.txt - Notepad". The window name is always "filename.extension - Notepad", so you can handle what you might need.

I suppose you could call System.IO.File.GetLastAccessTime (filePath). Then you can poll the file so often, and when the access time changes, you know that the file was opened, you can fire the event that the file was opened. See Jeffs post here: Finding a file read in C #

+1
source

You can also do this using the following tactics: It seems that the notepad has some kind of lock in the hosting folder (try deleting the folder and you will see that you cannot).

you can use the following code Using C #, how to determine which process has locked the file? to check the list of processes that lock the folder. one of the processes will be your notepad.

you could compare them by name as the other answers mentioned. if you open the file, you can save the PID and combine it with one of the returned processes.

0
source

All Articles