10 Replies - 5636 Views - Last Post: 31 August 2010 - 04:36 AM Rate Topic: -----

#1 Bez84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-October 09

Trying to check the last modified date of a text file

Posted 22 August 2010 - 03:26 PM

Hi,

In my app I want to have a timer control that every 5 seconds checks to see if a text file has been modified, and if so, calls a method. I just can't get my head around this problem and don't know why I'm getting the results I am. I'm very new to programming so no doubt it's some sort of school-boy error but if someone could highlight it for me I would be very grateful.

Here's the code:

        private DateTime fileMod = System.IO.File.GetLastWriteTime(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ClearNotes\USKBEmailer.txt");
        private DateTime lastMod = System.IO.File.GetLastWriteTime(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ClearNotes\USKBEmailer.txt");
        
        //USKBFeedTimer
        private void tmrUSKBEmailer_Tick(object sender, EventArgs e)
        {
                lastMod = System.IO.File.GetLastWriteTime(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ClearNotes\USKBEmailer.txt"); //So above Ive declared two variables and here Im trying to simply update the lastMod one and then compare to fileMod
                if (lastMod > fileMod)
                {
                    MessageBox.Show(lastMod.ToString() + " " + fileMod.ToString()); //troubleshooting MessageBox to show where it's going wrong
                   // USKBReader();
                }
            }



Now this seems to make sense to me, what I'm expecting is that if the file has not been modified (it hasn't) then surely it should just update the variable with the same value? But instead get the MessageBox text "22/08/2010 23:11:31 22/08/2010 23:10:56". I mean what the hell? If the file really hasn't been modified by any external forces why a different result from just updating the lastMod variable?

In case it's relevant, when the form is initialized I create the file and folder path if it's not there like so:
        public frmClearNotes()
        {
            System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ClearNotes");
            System.IO.StreamWriter objWriter = new System.IO.StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ClearNotes\USKBEmailer.txt");
            objWriter.Close();
            objWriter.Dispose();
            InitializeComponent();
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to check the last modified date of a text file

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Trying to check the last modified date of a text file

Posted 22 August 2010 - 05:23 PM

Have a look at the FileSystemWatcher, as it has functionality useful to you.
Was This Post Helpful? 3
  • +
  • -

#3 Bez84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-October 09

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 12:31 PM

OK so it appears I'm barking up the wrong tree, thank you.

Still I'd be interested to know why the above wouldn't work if anyone would be so kind?
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 01:02 PM

Do you get the message every 5 seconds? as if the file is getting updated? or do you just get it the first time?
Was This Post Helpful? 0
  • +
  • -

#5 Bez84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-October 09

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 01:14 PM

View Posteclipsed4utoo, on 30 August 2010 - 12:02 PM, said:

Do you get the message every 5 seconds? as if the file is getting updated? or do you just get it the first time?



Every 5 seconds.

And sorry, I'm not sure if I should raise a new topic but for FileSystemWatcher, How does one set the path property to the user's %appdata% folder?
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 01:27 PM

Here is my tutorial...

http://www.dreaminco...mwatcher-class/

You set the path using the Path property of the FileSystemWatcher class.

So it would be something like this...

FileSystemWatcher fw = new FileSystemWatcher();
fw.Path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
// the rest of the code


Was This Post Helpful? 0
  • +
  • -

#7 Bez84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-October 09

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 02:02 PM

ah yes thank you, it looks like I'm actually having trouble with the subfolder in that case.

So I want to only monitor a subfolder within the appdata folder and I have referenced this previously without problems programatically like so

System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)+ @"\ClearNotes\USKBEmailer.txt";



However as soon as the ap runs it says this path is invalid, yet if I take the subfolder part off so it's just

this.uskbWatcher.Path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);



that works fine, how do I reference the subfolder correctly?

(also tried)

this.uskbWatcher.Path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)+ "\\ClearNotes\\USKBEmailer.txt";


This post has been edited by Bez84: 30 August 2010 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 02:03 PM

It doesn't have to be a folder, it could be file.
Was This Post Helpful? 0
  • +
  • -

#9 Bez84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-October 09

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 02:07 PM

sorry just figured it out, I'm only supposed to put up to the folder not the file :blush:
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

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


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

Re: Trying to check the last modified date of a text file

Posted 30 August 2010 - 02:10 PM

One note I'd like to add. Check out the Path.Combine method. It's a convenient and safe way to make paths instead of just string concatenation.
Was This Post Helpful? 1
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Trying to check the last modified date of a text file

Posted 31 August 2010 - 04:36 AM

View PostBez84, on 30 August 2010 - 05:07 PM, said:

sorry just figured it out, I'm only supposed to put up to the folder not the file :blush:


To watch a specific file, you put the file as the Filter.

FileSystemWatcher fw = new FileSystemWatcher();
fw.Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"ClearNotes");
fw.Filter = "USKBEmailer.txt";

// the rest of the code


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1