2

I want to prompt a user to save a file when some modification has been done to it but the problem is i can't for the life of me do that.

Some people have suggested using FileInfo class but it only gives you the lastWriteTime, LastAccessTime and CreationTime.

I would like to use FileInfo class rather than FileSystemWatcher to check for modifications but how?

Example: Say a user has edited a file, within my program, that they loaded and clicks EXIT, i want a way to check whether any modifications were done on the file. If none, exit. If some, prompt user to save the file. So how do i check for modifications on that FILE?

cantfindaname88
  • 195
  • 1
  • 6
  • 15
  • 1
    We don't have enough information to answer this. Do you mean something like in VS itself, where the file gets an asterisk next to the name in the tab when you edit it, and then prompts the user to save the file when they close VS? – Chris Shain Mar 09 '12 at 19:46
  • 2
    Please clarify your question. Do you mean when your own program has modified the file? Or do you mean when some external program has modified it? If it's the second one, you're either going to need to poll it with FileInfo (check it again and again) or use FileSystemWatcher to notify you. – O. Jones Mar 09 '12 at 19:48
  • You want to _Save_ it when something else changed it on the Disk? Reloading seems to make more sense. – H H Mar 09 '12 at 19:48
  • Yeah i forgot to mention that the file i'm looking at is the one the user is editing. – cantfindaname88 Mar 09 '12 at 20:39
  • Your situation is still unclear. Is the user editing the file in your program? Are you trying to check if other changes were made separately by another program? Or are you just trying to see if the user edited the file within your program, and hasn't saved it yet? – tcovo Mar 09 '12 at 20:56
  • Within my program and the file was already saved. – cantfindaname88 Mar 09 '12 at 22:14
  • Say a user has edited a file, within my program, that they loaded and clicks EXIT, i want a way to check whether any modifications were done on the file. If none, exit. If some, prompt user to save the file. So how do i check for modifications on that FILE? – cantfindaname88 Mar 09 '12 at 22:17
  • Can you just use a variable in your program that keeps track of whether the file is modified since the last save? For example, `bool contentsModified`. Every time the user makes a change, set `contentsModified = true;`, every time they save, set `contentsModified = false;`. Then on exit, check `contentsModified`. – tcovo Mar 09 '12 at 22:32

2 Answers2

7

The easiest way is to calculate the MD5 hash of the file and compare to the original MD5 hash and if these two don't match the file was modified...

        using (var md5 = new MD5CryptoServiceProvider())
        {
            var buffer = md5.ComputeHash(File.ReadAllBytes(filename));
            var sb = new StringBuilder();
            for (var i = 0; i < buffer.Length; i++)
            {
                sb.Append(buffer[i].ToString("x2"));
            }
            return sb.ToString();
        }
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
1

Here are some examples of how to use the File or FileInfo class to get the LastWriteTime.

http://www.csharp-examples.net/file-creation-modification-time/

I would store the timestamp of the file when you load it, then compare it to the File.GetLastWriteTime() to see if the file has been saved since then. If the file was modified by an outside source, you can give the user the option to discard their changes and reload the file, or save their changes to a new file.

mgnoonan
  • 7,060
  • 5
  • 24
  • 27