CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    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?
    It's conjecture that the file is copied, but the open, locked file is synced to another location immediately after a change is saved. Whether it was by VSS or not, I can't say. I don't have access to the program code of the program. I didn't notice any difference between the open file and the saved, sync file, if there was any integrity issue. I don't know if an integrity issue would evolve after multiple syncs, either.

    I suppose it would work to sync a file after it has been released, it's just interesting that someone has done the sync work on the fly as changes are made. Whether that's advisable or not, I don't know. I just noticed that's it's being done. But wouldn't system notifications have to be ignored or otherwise dealt with as they pop up during the save process on locked files? That I don't know anything about. I also don't know anything about asynchronous processes, which is something else I'll have to deal with later, I think.

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: GetLastWriteTime

    My question is toward what you are trying to accomplish. You may have mentioned this in some other thread already.

  3. #18
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    My question is toward what you are trying to accomplish. You may have mentioned this in some other thread already.
    I think my objective is to create a backup utility that syncs between two folders on a pc in a timely manner. I suppose I'm comparing what I can create with other applications, but I'm learning what I want to produce as I find out what's possible. Overall, though, the primary objective is to provide that mens to backup information.

  4. #19
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: GetLastWriteTime

    Sounds like vss is the way to go.

  5. #20
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    Sounds like vss is the way to go.
    I have doubts about that after what you said about partial file info. I don't want to jeopardize file integrity by using a means like VSS if it leads to inferior process. Quality is preferable to expediency. I'd like to know anything you've got to tell me about this.

  6. #21
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: GetLastWriteTime

    Quote Originally Posted by KGCole View Post
    I have doubts about that after what you said about partial file info. I don't want to jeopardize file integrity by using a means like VSS if it leads to inferior process. Quality is preferable to expediency. I'd like to know anything you've got to tell me about this.
    I'm not sure I have anything left to add. Time to start coding and try out some different approaches.

  7. #22
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    I'm not sure I have anything left to add. Time to start coding and try out some different approaches.
    Sorry, had to be away from things for a while. On this subject, I put a method together to test whether the changed file is released, but I haven't been able to get the exception for the error ignored if it's still locked. The exception still interrupts the process and says the file is in an open process. So basically I don't know how to have the exception ignored. Here's the sub, don't know how I can change it to get the desired result:

    Code:
            Private Function VerifyLastWrite(e As FileSystemEventArgs) As Boolean
                Dim fileDone As Boolean
    
                Try
                    Dim fs As FileStream = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None)
                    fileDone = True
    
                Catch ex As Exception
                    fileDone = False
    
                End Try
                Return fileDone

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: GetLastWriteTime

    Quote Originally Posted by KGCole View Post
    Sorry, had to be away from things for a while. On this subject, I put a method together to test whether the changed file is released, but I haven't been able to get the exception for the error ignored if it's still locked. The exception still interrupts the process and says the file is in an open process. So basically I don't know how to have the exception ignored. Here's the sub, don't know how I can change it to get the desired result:

    Code:
            Private Function VerifyLastWrite(e As FileSystemEventArgs) As Boolean
                Dim fileDone As Boolean
    
                Try
                    Dim fs As FileStream = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None)
                    fileDone = True
    
                Catch ex As Exception
                    fileDone = False
    
                End Try
                Return fileDone
    You need to put the FileStream check inside a using block like it is in my example code. The using block will cause the FileStream to release the file object when it goes out of scope (which is immediately at the end of the using block). If you don't have a using block, the FileStream object you are using to check the file will itself keep a lock on the file.

    From my code snippet:
    Code:
    try 
    { 
      using( FileStream fs = new FileStream( args.FullPath, FileMode.Open, FileAccess.Read, FileShare.None ) ) 
      { 
        // Debug.WriteLine( args.FullPath );
        transferCompleted = true; 
      } // IMPORTANT: the using block will release the FileStream object (and the lock for this object) here
    } catch( IOException ) 
    { 
      // Ignore exceptions where we can't open the file 
      // This is because the file is still being copied 
    }
    Last edited by Arjay; November 7th, 2018 at 08:07 AM.

  9. #24
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    You need to put the FileStream check inside a using block like it is in my example code. The using block will cause the FileStream to release the file object when it goes out of scope (which is immediately at the end of the using block). If you don't have a using block, the FileStream object you are using to check the file will itself keep a lock on the file.
    I'm not sure I constructed this using statement correctly. My code still gets interrupted with the locked process error. Could you tell me what is wrong with this snippet?

    Code:
            Private Function VerifyLastWrite(e As FileSystemEventArgs) As Boolean
                Dim fileDone As Boolean
    
                Try
                    Using fs As FileStream = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None)
                        fileDone = True
    
                    End Using
                Catch ex As Exception
                    fileDone = False
    
                End Try
                Return fileDone
    
            End Function

  10. #25
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: GetLastWriteTime

    What happens if you debug and step through the code?

  11. #26
    Join Date
    Sep 2018
    Posts
    38

    Re: GetLastWriteTime

    Quote Originally Posted by Arjay View Post
    What happens if you debug and step through the code?
    If I put a breakpoint on the using declaration and let it run to that point, then run the code through after the break, I don't get the error. Don't know quite what to make of that. If I don't have a breakpoint, I always get the error...

Page 2 of 2 FirstFirst 12

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
  •  





Click Here to Expand Forum to Full Width

Featured