dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: GetLastWriteTime

  1. #1
    Join Date
    Sep 2018
    Posts
    38

    GetLastWriteTime

    I have a line of code in an onChanged event for a FileSystemWatcher, and have not gotten the result that I want. Here's the code:

    Code:
                    str = "SELECT Base, Target FROM Backup WHERE Base = " & "'" & getDir & "'" & "OR Target = " & "'" & getDir & "'"
                    cmd.Connection = myConn
                    cmd.CommandText = str
                    myConn.Open()
    
                    Dim lstReader As OleDbDataReader = cmd.ExecuteReader()
    
                    lstReader.Read()
                    baseDir = lstReader.Item(0).ToString()
                    targetDir = lstReader.Item(1).ToString()
                    baseTime = IO.File.GetLastWriteTime(lstReader.Item(0).ToString() + "\" + e.Name)
                    targetTime = IO.File.GetLastWriteTime(lstReader.Item(1).ToString() + "\" + e.Name)
                    MessageBox.Show(baseTime.ToString() + "--" + targetTime.ToString())
    I'm finding that the GetLastWriteTime that is returned to me is not the lastest write time; for instance, I open a graphics file, change it, and then save it, but the last write time that is returned to me from the onChanged event is not the write time from my last save, but earlier. Does anyone know why this may be? I didn't post the whole sub, but if anyone needs me to I will.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    The code you posted is db code, not fsw code. In your fsw code, be sure to subscribe to the last change event.

  3. #3
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    The code you posted is db code, not fsw code. In your fsw code, be sure to subscribe to the last change event.
    I'm not sure what you mean by subscribe to the last change event, Arjay.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    Look at the msdn docs for fsw. You can enable different events like onCreated, onUpdated, onDeleted, etc. I believe you can do the same for LastChanged.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    Actually, you set up a NotifyFilter.LastWrite. Take a look at the msdn docs - it has an example.

  6. #6
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    Actually, you set up a NotifyFilter.LastWrite. Take a look at the msdn docs - it has an example.
    I read through the fsw material again, but that's pretty much what I have coded. I'm still wondering how to take into account that multiple events are delivered when one file is changed; for instance, I change a graphics file, save it, and get four different notifications for the event. I've been trying to figure out how I can weed out the last notification which is the most recent one.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    Quote Originally Posted by KGCole View Post
    I read through the fsw material again, but that's pretty much what I have coded. I'm still wondering how to take into account that multiple events are delivered when one file is changed; for instance, I change a graphics file, save it, and get four different notifications for the event. I've been trying to figure out how I can weed out the last notification which is the most recent one.
    I've been using the fsw for quite a while. Previously, I was subscribing several different NotifyFilters and the Changedand Created events. To determine if it was the *last event, I would attempt to open the file with an exclusive lock for read access. I didn't want to do anything at that point with the file, I just wanted to get the lock. If I could get the lock, then I could be as sure as possible that the external operation was finished writing to the file (I don't have this code handy, but I can post this approach later).

    Recently, on another project, I needed a fsw and started to copy my old code. The previous code handled large files, and the new code handled (relatively) tiny files. I ran into an article that simply does the following in a test project:
    Code:
    static void Main(string[] args)
    {
       var watcher = new FileSystemWatcher();
                
       watcher.Path = @"path goes here";
       watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
    
       // Only watch text files.
       watcher.Filter = "*.txt";
    
       // Add event handlers.
       watcher.Changed += OnChanged;
    
       // Begin watching.
       watcher.EnableRaisingEvents = true;
                
       Console.WriteLine("FileSystemWatcher test. Press any key quit the sample.");
       Console.Read();
    
    }
    
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
      Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
    }
    The above code worked fine and seemed to give me a single event after the file gets written. However, in my testing today (before posting this code), it doesn't trigger when I paste a file into the folder.

    So let me dig up the old code that does the check with the file lock. Sorry about the C# code, but I don't code in VB.Net.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    Here is the code for the locked file check..
    Code:
    private void InitializeFileSystemWatcher( )
    {
      _fileSystemWatcher = new FileSystemWatcher( _destinationFolder );
    
      _fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
      _fileSystemWatcher.EnableRaisingEvents = true;
      _fileSystemWatcher.Changed += new FileSystemEventHandler( OnChanged );
    }
    
    protected virtual void OnChanged( object sender, FileSystemEventArgs args )
    {
      if( VerifyFileTransferComplete( args) )
      {
        // Do work with file
      }
    }
    
    private bool VerifyFileTransferComplete( FileSystemEventArgs args )
    {
      bool transferCompleted = false;
    
      try
      {
        using( FileStream fs = new FileStream( args.FullPath, FileMode.Open, FileAccess.Read, FileShare.None ) )
        {
          // Debug.WriteLine( args.FullPath );
          transferCompleted = true;
        }
      }
      catch( IOException )
      {
        // Ignore exceptions where we can't open the file
        // This is because the file is still being copied
      }
      return transferCompleted;
    }

  9. #9
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    Here is the code for the locked file check..
    Thanks Arjay, I'll look this over and see how I can implement the idea to solve my notification issues. I'll probably have questions later on.

  10. #10
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    Here is the code for the locked file check..
    I integrated your idea into my code, Arjay...I'm having a problem with file access, though. I've got a file open in a graphics editor, and when I save the file the right notifications come up, but I can't copy the file because it's still open in that process. Do you know any way around that?

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    Quote Originally Posted by KGCole View Post
    I integrated your idea into my code, Arjay...I'm having a problem with file access, though. I've got a file open in a graphics editor, and when I save the file the right notifications come up, but I can't copy the file because it's still open in that process. Do you know any way around that?
    If a program has a file locked because it's using the file, your file watcher program won't be able to do anything until the first program releases the file. This is true for any program that has an exclusive lock on a file, i.e., other programs won't be able to access the file until the lock is released. Some programs aren't real good about immediately cleaning up file handles after the user closes the file in the program (but keeps the program running). In this case, the lock stays until the program is closed. I would avoid the scenario where you are monitoring files in folders where the user is saving them with programs that retain locks.

  12. #12
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    If a program has a file locked because it's using the file, your file watcher program won't be able to do anything until the first program releases the file. This is true for any program that has an exclusive lock on a file, i.e., other programs won't be able to access the file until the lock is released. Some programs aren't real good about immediately cleaning up file handles after the user closes the file in the program (but keeps the program running). In this case, the lock stays until the program is closed. I would avoid the scenario where you are monitoring files in folders where the user is saving them with programs that retain locks.
    I dunno, Arjay...I know what you mean about a file being locked. I've run into that scenario on a general basis with open files. But I downloaded a popular syncing program on a trial basis to see what it did with open files, and it was able to sync the open file with no problem. So I guess there's a way to do this. I don't know what it is, but it can be done. I'll have to think about this, and do some more research. I'll post my results if I find anything regarding it.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    When you say the trial program synced the files, what does that mean? Did it copy an open file to a different location it was changed (but locked) or did it cooy after the file was unlocked. Copying files that are in use but changed is an easier problem to solve vs. processing a file after its last change. Withe the former, you don't care if the file is complete because it eventually will be, but that is a very different thing from waiting until the last change to process. My requirement was was for the 2nd scenario. You'll need to alter or change the code for your requirement.

  14. #14
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    When you say the trial program synced the files, what does that mean? Did it copy an open file to a different location it was changed (but locked) or did it cooy after the file was unlocked. Copying files that are in use but changed is an easier problem to solve vs. processing a file after its last change. Withe the former, you don't care if the file is complete because it eventually will be, but that is a very different thing from waiting until the last change to process. My requirement was was for the 2nd scenario. You'll need to alter or change the code for your requirement.
    It copied the open file to the target location while it was open and immediately after it's last saved change. I've done some reading on this over the net and I think how this is being accomplished is by way of VSS shadow file system. I think the shadow image of the last change is in the VSS file system by way of Windows that crawls the file system for changes and updates the volume image, and that file image is what is accessed and copied before a locked file is closed and released.

    I've also run into some C# code that a poster showed as being a cmd line copy using the Process class of System namespace. I'll post that here for you to look at. I haven't had a chance to try it in a C# codepage yet, but I know that's your language forte', Arjay. Here's the code snippet...well, couldn't copy and paste it so here's the link to the page with the info:

    https://stackoverflow.com/questions/...nother-process

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,807

    Re: GetLastWriteTime

    Can you explain (again) what happens to the file after the last change is detected and why it is important to work with a locked file?
    Last edited by Arjay; October 28th, 2018 at 04:40 PM.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)