CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    73

    Question Read the content of a file before it is being changed

    Hi,

    I'm monitoring a directory to see if there are any changes in its content. If a certain file in this directory is being changed, or overriden, I want to be able to read its content before it have been changed with the new content.

    Naturally, I tried using FileSystemWatcher class, but it allerts me that the file changed after it have changed, and the old content is already gone.

    Can someone point me to a solution, proferrably another C# class which provide the functionality I'm looking for?

    Thanks

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Read the content of a file before it is being changed

    Hi,

    Why can't you keep a copy of the file at beginning of the monitoring process? Then each time it changes you'll be notified. When you receive the notification make another copy of the file. That way each time the file changes you can always look at the content of the previous version as you have a copy of it. Alternatively, you can just read the contents and store them instead of copying the file....
    Last edited by nelo; April 25th, 2009 at 05:05 PM. Reason: I need to correct a grammatical error...:)

  3. #3
    Join Date
    Apr 2009
    Posts
    73

    Question Re: Read the content of a file before it is being changed

    Hi,

    Thanks for the reply.

    Saving a copy is not good enough when you're dealing with large scale directories, and don't want the first copy of all the files to take 10 or more minutes.

    Any other solutions?

    Thanks

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Read the content of a file before it is being changed

    Hi,

    Does it really matter how long it takes? The first will only need to happen once per file for the duration of the application. 10 minutes doesn't sound like a lot in terms of setup/initialisation time.

  5. #5
    Join Date
    Apr 2009
    Posts
    73

    Question Re: Read the content of a file before it is being changed

    Hi,

    Basically, you're right.

    I would still like to see if there's any other solution to the problem.

    Thanks.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Read the content of a file before it is being changed

    This would require explicit support by the filesystem, which isn't there. Therefore there's no easy way to do this. The easiest way to accomplish what you want would be to duplicate all the files/directories that you were monitoring and then just compare those versions to the changed versions when the file changes.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

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