Results 1 to 8 of 8

Thread: Check if File Changed on Form Load

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    11

    Check if File Changed on Form Load

    Hey everyone,
    I'm trying to figure out how to check if a file was changed when Form1 Loads. If the file was changed (for instance, if the file was renamed to something else), then a MsgBox ("File Changed!") would appear. However, if the file hasn't changed since the program shutdown, then it would say ("File OK").

    When the program starts, you have to Browse for the file using an OpenFileDialog, and my program checks to make sure the file contains "\filename.exe", regardless of the folder location. If the user browses for that specific file and finds it, the program saves that file location in the textbox upon the program shutting down in My.Settings. When the program starts up again, it loads that file location. All of that works great.

    The problem is if I rename the file in the directory to something different, such as "Filename2", then the program won't detect that the file was renamed, and will still say "File OK". I'm assuming this is because it's saving the previous location in My.Settings and loading it, but how can I detect if the file was renamed?

    This is my code. Also, "txtfiledirectory" is the textbox that outputs the file location directory and shows where the file is located. "btnBrowse" is the Browse button which launches the OpenFileDialog and checks for the file. "LoadFilesButton" is a button that when clicked checks the file from the OpenFileDialog.

    Code:
      
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     
            Me.txtFileDirectory.Text = My.Settings.MyFileLocationSave
    
     End Sub
    Code:
    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
    
    
            Dim myFileDlog As New OpenFileDialog()
    
            'look for files in the c drive
            myFileDlog.InitialDirectory = "c:\"
    
            'specifies what type of data files to look for
            myFileDlog.Filter = "All Files (*.*)|*.*" & _
                "|.exe (*.exe)|*.exe"
    
    
            'specifies which data type is focused on start up
            myFileDlog.FilterIndex = 2
    
            'Gets or sets a value indicating whether the dialog box restores the current directory before closing.
            myFileDlog.RestoreDirectory = True
    
            'seperates message outputs for files found or not found
            If myFileDlog.ShowDialog() = _
                DialogResult.OK Then
                If Dir(myFileDlog.FileName) <> "" Then
                    MsgBox("File Exists: " & _
                           myFileDlog.FileName, _
                           MsgBoxStyle.Information)
                Else
                    MsgBox("File Not Found", _
                           MsgBoxStyle.Critical)
                End If
            End If
    
            'Adds the file directory to the text box
            txtFileDirectory.Text = myFileDlog.FileName
    
            'Set value
            My.Settings.MyFileLocationSave = myFileDlog.FileName
    
            'Load value
            Dim F = My.Settings.MyFileLocationSave
    
            'Persist values
            My.Settings.Save()
        End Sub
    Code:
    
        Private Sub LoadFilesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFilesButton.Click
            If InStr(1, txtFileDirectory.Text, "\filename.exe", vbTextCompare) Then
                MsgBox("File OK!")
      
            Else
                MsgBox("File changed!")
            End If
        End Sub
    Thanks for the help!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,954

    Re: Check if File Changed on Form Load

    The obvious thing to me to do is to save the file path and the last modified time. Next time you start up, look for that path and, if you find it, check the last modified time against the value you have stored. If you don't find the path or the last modified times don't match, the files have changed.

    That said, a user can manipulate the last modified time of a file so it may be even better to calculate, store and check an MD5 hash for the file instead.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,471

    Re: Check if File Changed on Form Load

    If you want to check if someone changed the file name outside of to program then you should save the file name also in My.Settings in a separate variable .

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    11

    Re: Check if File Changed on Form Load

    Quote Originally Posted by wes4dbt View Post
    If you want to check if someone changed the file name outside of to program then you should save the file name also in My.Settings in a separate variable .
    Yeah, I've sort of already tried that, but the problem is that when the application is run after a file is renamed, it's still storing the old value that it saved. Somehow, that value needs to be checked for any changes. I tried looking at FileSystemWatcher, but that won't watch the specific file since it's meant for directories.

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,471

    Re: Check if File Changed on Form Load

    Quote Originally Posted by CountySheriff View Post
    Yeah, I've sort of already tried that, but the problem is that when the application is run after a file is renamed, it's still storing the old value that it saved. Somehow, that value needs to be checked for any changes. I tried looking at FileSystemWatcher, but that won't watch the specific file since it's meant for directories.
    Yeah, but you can change that. If the file has been renamed, you could just prompt the user and ask them if they want to update the file name setting.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,954

    Re: Check if File Changed on Form Load

    It seems to me that you're complicating this. The steps seem fairly simple to me.

    1. Add settings to your application for file path and either last modified time or MD5 hash.
    2. When you run, check whether the file path setting has a value.
    3. If there's no file path, prompt the user to select a file, then save the path and time/hash to your settings.
    4. If there is a file path, check whether the file exists.
    5. If there's no file path, prompt the user to select a file, then save the path and time/hash to your settings.
    6. If there is a file, check whether the last modified time or hash match and do whatever is appropriate based on the result.

    Note that, if a user renames a file, there's no way for you to identify the file without their selecting it again and there's no way for you to know that it's the same file that they select, although a matching MD5 indicates that the file content is the same.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    11

    Re: Check if File Changed on Form Load

    Quote Originally Posted by jmcilhinney View Post
    It seems to me that you're complicating this. The steps seem fairly simple to me.

    1. Add settings to your application for file path and either last modified time or MD5 hash.
    2. When you run, check whether the file path setting has a value.
    3. If there's no file path, prompt the user to select a file, then save the path and time/hash to your settings.
    4. If there is a file path, check whether the file exists.
    5. If there's no file path, prompt the user to select a file, then save the path and time/hash to your settings.
    6. If there is a file, check whether the last modified time or hash match and do whatever is appropriate based on the result.

    Note that, if a user renames a file, there's no way for you to identify the file without their selecting it again and there's no way for you to know that it's the same file that they select, although a matching MD5 indicates that the file content is the same.
    Do you know how I could get it to assign an MD5 hash?

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,954

    Re: Check if File Changed on Form Load

    There are lots and lots of examples around for computing MD5 hashes. It's quite a common task. Here's a simple one I found that was the fifth result in a web search, so it took me about a minute to find it:

    https://warren5236.wordpress.com/200...hash-in-vbnet/

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