9 Replies - 2952 Views - Last Post: 20 January 2012 - 11:36 PM Rate Topic: -----

#1 scolty   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Writing to a txt file when its already open

Posted 20 January 2012 - 05:37 AM

Afternoon,

Im wondering if its possible to actually check if another program has access to a file while ur writing to it? See screenshot for example

Posted Image

I wrote a section of code which checks if a file is open
public bool FileExist(string fileLocation)
        {
            try
            {
                FileStream fs = File.Open(fileLocation, FileMode.Open, FileAccess.Write);
                fs.Close();
                return true;
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("Incorrect file path was specified. Error Message: {0}", e.Message);
                return false;
            }

            catch (FileLoadException e)
            {
                Console.WriteLine("Cant load the file. Error Message: {0}", e.Message);
                return false;
            }
        }



I then opened the error file and changed it. Saved it. Ran the program and it wrote to the file and opened it. See upper NotePad in screenshot. I then closed that saveed the 2nd NotePad (Bottom one) and closed that. When i go to reopen the file, the info from the 2nd NotePad is displayed and the 1st one is missing.

Im assuming since the method i wrote didnt highlight that the file was already open, that NotePad stores the contents temporarily and then closes the file. Only reopening it when i save it.

[Question]:If thats the case, is there anyway to know that it has the file open (i know its not technically open but u know what i mean)??

Thanks

This post has been edited by scolty: 20 January 2012 - 05:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Writing to a txt file when its already open

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 06:30 AM

I would love to help, but I can't tell what your problem is.

So you tried to write to a text file that you already had opened. It wrote to it without an error, but the text doesn't show up in the file?
Was This Post Helpful? 1
  • +
  • -

#3 scolty   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 06:44 AM

Sorry for not making it clear enough. Let me start again.

Problem:Im trying to find out if the file is open already.

So i wrote the above method to determine if the file was already open.

I then opened the file in Notepad.

Ran the method and it returned "True" = File not open.


I have another method which writes to the file called SampleCSV2.txt. so i openned that file. Wrote some text in it and saved it.

Ran the method which writes to SampleCSV2.txt.

Closed the modified copy i had open and saved the changes.

When i open the file again, it shows me text i modified ,ie its as though the method hadnt written anything to the file.

Does that make sense?? If not let me know and i will get some screenshots of the sequence.

Thanks for taking the time to reply :)
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 06:59 AM

There is more than just 'open'.

Open.Read can be done more than than one process. Several processes can read at the same time.

Open.Write can only be done by one process. If you try to open.Write and get an exception then it is probably already open for writing.

You also have to understand how programs like Notepad work. Notepad does not keep a file open. It reads all the data to memory and closes its connection. The file is not open even though you see the data on screen. When you tell it 'save' it then does an Open.Write to writes all its data.

Sometimes its not our software that's off, but our assumptions.
Was This Post Helpful? 1
  • +
  • -

#5 scolty   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 07:15 AM

Thanks for replying. I modified it to the following

class csvchecker
{
public bool FileExist(string fileLocation)
        {
            try
            {
                FileStream fs = File.OpenWrite(fileLocation);
                fs.Close();
                return true;
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("Incorrect file path was specified. Error Message: {0}", e.Message);
                return false;
            }

            catch (Exception e)
            {
                Console.WriteLine("File may contain errors. Error Message: {0}", e.Message);
                return false;
            }
        }
}



Note: catch (Exception e) is just there so that i caught everything as i wasnt sure UnauthorizedAccessException would be fired when i had the file open in NotePad.

Anyway, with the file open again, it still doesnt throw an exception. Regarding your point about it not maintaining the file open but instead reads the file into memory. Is it actually possible to know if NotePad or similar program has stored the data and is still running?

calling code is as follows:
class Program
    {
        static void Main()
        {
            string fileLocation;
            string fileLocation2;
            double sizeOfFile;
            bool check;
            csvchecker CSV;


            do
            {

                Console.WriteLine("Please enter the path for the .CSV file");
                fileLocation = Console.ReadLine();
                Console.WriteLine("New file location:");
                fileLocation2 = Console.ReadLine();

                CSV = new csvchecker();

                check = CSV.FileExist(fileLocation);
            }
            while (check == false);
            
            sizeOfFile = CSV.FileSize(fileLocation);
            CSV.CheckingCSV(fileLocation, fileLocation2);

        }
    }



thanks again for your help
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 08:21 AM

You can check if the application is running.

But unless a developer specifically wrote in the ability and and tracking to report what it last (currently) opened there isn't a way of knowing. There isn't some standard that all coders have to adhere. One guy might do it, the next not, the next does, but in a different wy than the first.

Most programs don't let outsiders just looking inside and tell you what is stored.
Was This Post Helpful? 1
  • +
  • -

#7 scolty   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 08:24 AM

So... the answer is no. Although u state that its possible to see if the application is running (NotePad in this instance) its impossible for me to know if it had the file already openned and stored in memory??

Thanks again for your help on this matter, i really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 10:19 AM

For the record, you have to understand how Notepad is actually working. It's not maintaining a write lock on the file it has "open." In fact, it's not holding that file open at all. It quickly reads the file into memory (with a read lock), and closes it. It never gains a write lock until it's ready to write the file (as in, someone clicked "save"). Then, it opens the file with a write lock, writes the file, and closes the file and the lock.

Thus, the file is not actually open. Your application can't detect that it's open, because Notepad doesn't have the file open. It has a representation of the file open, and a path to overwrite when it saves.

Edit: sorry tlhIn`toq, I just skimmed the thread. Didn't notice that you had already posted to this effect.
Was This Post Helpful? 2
  • +
  • -

#9 blogwhodle   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 06:44 PM

Could you use File.GetLastAccessTime somehow???
:hammer:
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Writing to a txt file when its already open

Posted 20 January 2012 - 11:36 PM

Well, that would let you know when it was last touched, but not necessarily that it's "open" in a program like notepad. Basically, the only way you'd be able to do this is to get the open windows of notepad and check the titles.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1