2

how to find whether specific .txt file is opened in notepad?

I have tried solutions mentioned here

Is there a way to check if a file is in use?

But they work fine for Word and pdf file but not working for txt file opened in Notepad.

here is code I have 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;
} 

am i missing somthing here.??

My requirement: I have application which works similar to VSS. When user checks out specific file and opens ,and try to check in the same, while it has opened. Application is suppose to throw a warning message.For that i have used the above functionality.Its working fine for word and pdf.

Community
  • 1
  • 1
Vijay Hulmani
  • 969
  • 8
  • 17
  • 11
    I believe that is because Notepad just opens the file, reads the stream and closes the stream, therefore not locking the file. However Word will keep the file opened and locked. – Belogix Aug 06 '13 at 13:17
  • Belogix is correct, the file is not in use, therefor your code is working properly – x4rf41 Aug 06 '13 at 13:18
  • 2
    What are you trying to achieve by doing this? As has been commented this approach will fail for any application that loads/reads/closes – Alex K. Aug 06 '13 at 13:19
  • 2
    What is your end goal? Why are interested in knowing if a file is opened in notepad if it does not affect the file? – Khan Aug 06 '13 at 13:19
  • not possbile, the only thing close would be to check the window title of all open notepads, but they only contain the filename, not the path, so it wouldnt really work – x4rf41 Aug 06 '13 at 13:19
  • i have application similar to vss. when user checks out specific file and opens ,try to check in the same while it has opened. I am suppose to throw a warning message.For that i have used the above functionality.Its working fine for word and pdf.But not for notepad. – Vijay Hulmani Aug 06 '13 at 13:22

3 Answers3

11

To expand on my comment. A file is only locked if a handle is kept open by an application. Word for example will open the file, read in the stream and maintain the handle so that other applications cannot delete that file while the user is working on it.

Notepad, and other applications, just open the file, read in the entire stream and then close the file releasing 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 will not care as it has its own copy in memory.

You could try and hack around with getting instances of Notepad and checking if a file is open but this is ultimately not a great idea. If the file is not locked then you should be free to do what you want with it.

Belogix
  • 8,129
  • 1
  • 27
  • 32
  • 5
    Not sure if I should upvote this, because "2,222" is such a nice number ;) – Drew McGowen Aug 06 '13 at 13:20
  • 4
    I know, I've been posting most of my answers as comments because I like 2,222 but I thought 3,333 looks even better! ;-) – Belogix Aug 06 '13 at 13:21
  • There isn't a mechanism to determine if the file is open from an instance of the process as the process is not interacting with the file after its been read. – Alex K. Aug 06 '13 at 13:21
  • 1
    Well it has to somehow hold on to *at least* the file path - otherwise Ctrl+S wouldn't work. – Drew McGowen Aug 06 '13 at 13:22
  • @AlexK. - You are right but you can get the Title of the application so "Notepad - My Sample File.txt" but a hack and why I advised against it! – Belogix Aug 06 '13 at 13:23
  • What @DrewMcGowen said is true, you could scan the process memory for the filename (but not with C# i think), if you know the offset (which probably is always the same) you could get the opened file path. but thats is a bit overkill i think – x4rf41 Aug 06 '13 at 13:25
  • I suppose you could look at the processes command line (http://stackoverflow.com/questions/2633628/can-i-get-command-line-arguments-of-other-processes-from-net-c) to achieve this, but only for files opened via the shell (as opposed to file->open) – Alex K. Aug 06 '13 at 13:25
  • @Belogix:i have added my requiremnet why i am doing that. – Vijay Hulmani Aug 06 '13 at 13:32
2

This is a hack solution I just came up with, but it should work for you. This makes use of 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();

Hopefully that should do the trick. My example looks to see if an instance of notepad is open with the window title "myFile.txt - Notepad". The window name is always "filename.extension - Notepad" so you can handle that however you might need to.

I suppose you could make a call to System.IO.File.GetLastAccessTime(filePath). You could then poll the file every so often and when the access time changes you know the file has been opened, you can then fire an event that the file has been opened. See Jeffs post here: Detect File Read in C#

Community
  • 1
  • 1
Daniel Lane
  • 2,575
  • 2
  • 18
  • 33
  • The only problem is what if I have a file `C:\Pete.txt` and `C:\Test\Pete.txt`. Which one is open in Notepad? – Belogix Aug 06 '13 at 13:35
  • I've amended my answer with something that may potentially work for Vijay. – Daniel Lane Aug 06 '13 at 14:42
  • I like the new idea but that will only help you know when it was last accessed, not if the user still has it open? They might have closed it immediately or minimised it and forgotten about it and therefore the file would still be open. – Belogix Aug 06 '13 at 14:52
  • Belogix this an instance of not having your cake and eating it. As others have said, notepad doesn't hold an open file handle. My suggestions are hack jobs at best but are probably better than nothing if it's REALLY required. – Daniel Lane Aug 06 '13 at 15:25
  • It's worked Thanks, @DanielLane for saving my time. – Krishnakumar Patel May 21 '21 at 14:26
0

You could also do this using the following tactic: It seems that notepad does hold some kind of lock on the hosting folder (try to delete the folder and you'll see you can't).

you could use the following code Using C#, how does one figure out what process locked a file? to check list of processes that lock the folder. one of the processes will be your notepad.

you could them compare by Title as another answers mentioned. if you're issuing the open of the file - you could save the PID and comapre it with one of the processes that returned.

Community
  • 1
  • 1
ArielB
  • 1,184
  • 2
  • 11
  • 36