How do you update the timestamp on the file when no changes have been made?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fachagooch
    New Member
    • Feb 2010
    • 6

    #1

    How do you update the timestamp on the file when no changes have been made?

    When i click on the Save button in the interface it executes the SaveData() steps. but i am being told that i need to update the date stamp on the Access file. any ideas as to how i would make this change?

    Code:
        Private Sub ProblemTableBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProblemTableBindingNavigatorSaveItem.Click
            SaveData()
        End Sub
        Private Sub SaveData()
            With Me
                .Validate()
                .ProblemTableBindingSource.EndEdit()
                .TableAdapterManager.UpdateAll(.GlobalSystemInfoDataSet)
                Write_Data()
            End With
        End Sub
        Private Sub Write_Data()
            Dim fs As New FileStream("C:\final\testdata.txt", FileMode.Append, FileAccess.Write)
            Dim sw As New StreamWriter(fs)
            Dim domainName As String = Environment.UserDomainName
            Dim userName As String = Environment.UserName
            sw.WriteLine(DateTime.Now + " " + domainName + "\" + userName + "  ---CHANGE MADE WILL EVENTUALLY GO HERE")
            sw.Close()
            fs.Close()
        End Sub
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    *Which* timestamp on the file? A file has several: Creation, modified and accessed.

    The accessed time will change automatically when you write your data.

    Otherwise you want to change the FileInfo for the file.

    Comment

    • fachagooch
      New Member
      • Feb 2010
      • 6

      #3
      The Modified Time stamp.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Oh I just *knew* you were going to say that. The Modified time stamp stinks. Different application do different things with it... different versions of Windows treat it differently than other versions... It has always been a nightmare at my employer's.

        In theory you don't have to do anything directly. When you modify the file's content, the O.S. will update this. Why do you think you need to do it expressly through code? Have you confirmed looking at the file's properties that the Modified timestamp is not being updated when your program makes changes?

        Here's a little experiment for you to try.
        • On the desktop right-click and make a new Text file.
        • Right-click on it and get properties. Write down all three timestamps.
        • Wait about a minute.
        • Open the file and make changes then save and close.
        • Check the properties and write down the times. See, the modified time is changed
        • wait about a minute
        • Copy/paste the document on the desktop so you have a duplicate of it.
        • Check the duplicate's properties.
        • Notice that you created less than a minute ago, yet you modified it about 2 minutes ago. Wow - you modified the file before you created it.


        Anyway.. I digress. My earlier suggestion of changing the FileInfo for the file still remains the way you need to do it.

        Comment

        • fachagooch
          New Member
          • Feb 2010
          • 6

          #5
          i am going to try to adapt this class to my VB code. will let you know if it works.

          Code:
          Imports System
          Imports System.IO
          
          Public Class Touch
              Public Shared Sub Main(ByVal args() As String)
          
                  ' Make sure an argument (filename) was provided.
                  If args.Length > 0 Then
          
                      ' Verify that the provided filename exists.
                      If File.Exists(args(0)) Then
                          Dim fi As FileInfo = New FileInfo(args(0))
                          touchFile(fi)
                      Else
                          Console.WriteLine("Could not find the file {0}", args(0))
                      End If
                  Else
                      Console.WriteLine("No file specified.")
                  End If
              End Sub
          
              Public Shared Sub touchFile(ByVal fsi As FileSystemInfo)
                  Console.WriteLine("Touching: {0}", fsi.FullName)
          
                  ' Update the CreationTime, LastWriteTime and LastAccessTime.
                  Try
                      fsi.CreationTime = DateTime.Now
                      fsi.LastAccessTime = DateTime.Now
                      fsi.LastWriteTime = DateTime.Now
                  Catch e As Exception
                      Console.WriteLine("Error: {0}", e.Message)
                  End Try
          
              End Sub
          
          End Class

          Comment

          • fachagooch
            New Member
            • Feb 2010
            • 6

            #6
            Code i used, pretty simple now that i look at it.

            Thanks for the help..

            Code:
                            Dim fi As FileInfo = New FileInfo("C:\testdata.txt")
                            fi.LastAccessTime = DateTime.Now
                            fi.LastWriteTime = DateTime.Now

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Good job! I hope you don't feel I made you jump through too many hoops on that. Yeah, I could have just given you the 3 lines of code... But learning by doing and researching and a little trial and error just seems to stick with a person a lot more than copy/paste.

              Comment

              • fachagooch
                New Member
                • Feb 2010
                • 6

                #8
                Hey that is the only way one learns!

                Comment

                Working...