GetLastWriteTime inaccurate

I use the function to check timestamps on files on remote computers. We have a lot of computers.

What I'm launching is a little strange.

I am running a query on a large number of computers. Timestamp on computer26887 appears as "1/4/2013 12:46:01 AM" I UNC on this computer and check the file. In the explorer, the time stamp says "9/16/2013 11:23" (one and a half hours ago) Hmmm .... The request again is the same old, incorrect, time stamp. I right-click on the file, go to properties and click "Details" in the properties window - "Modified on 9/16/2013 11:23 AM" Request again with vb code - now it shows the correct timestamp ????

I have hundreds of systems to go through, and if I can’t trust the data that I get, I have too much work!

Any ideas?

Update
Basically, VB.NET retrieves the cached version of the timestamp. The timestamp is updated, but the cache still has the old timestamp. How to make the cache update without manually opening the file properties in Explorer?

Shared Function GetFileInfo(ByVal ComputerName As String, ByVal FiletoFind As String, info As String) Dim Ret As String = "" Dim targetfile = "\\" & ComputerName & "\" & FiletoFind Dim fi As FileInfo = New FileInfo(targetfile) If fi.Exists Then fi.refresh Select Case info Case Is = "Exists" Ret = fi.Exists.ToString Case Is = "Delete" fi.Delete() Ret = fi.Exists.ToString Case Is = "Created" Ret = fi.CreationTime.ToString("MM/dd/yyyy hh:mm:ss tt") Case Is = "Access" Ret = fi.LastAccessTime.ToString("MM/dd/yyyy hh:mm:ss tt") Case Is = "Mod" Ret = fi.LastWriteTime.ToString("MM/dd/yyyy hh:mm:ss tt") End Select Else Ret = "File Not Found" End If Ret = Ret.Replace(vbCrLf, "") Ret = Ret.Replace(vbCr, "") Return Ret End Function 

(I also tried using File , not FileInfo ... to check message history)

UPDATE
As a test, I checked the file check on the system using AutoIT3 code. He returned the exact information. After checking AutoIT3, vb.net returned the exact timestamps. So what is the problem with vb.net that AutoIT3 does a better job?

 Func _timestampchk($path) Dim $file,$astamp $file = $path $astamp = FileGetTime($file, 0, 0) If IsArray($astamp) Then $stamp = $astamp[1] & "/" & $astamp[2] & "/" & $astamp[0] & " " & $astamp[3] & ":" & $astamp[4] ElseIf $astamp = 0 Then $stamp = "File " & $path & " not Found" Else $stamp = 0 EndIf Return $stamp EndFunc ;==>_timestampchk 
+4
source share
1 answer

Try FileSystem.GetFileInfo as pointed out by Andrew Morton and neoistheone

 Dim information = My.Computer.FileSystem.GetFileInfo("C:\MyLogFile.log") information.Refresh() ' Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated. MsgBox("The file full name is " & information.FullName & ".") MsgBox("Last access time is " & information.LastAccessTime & ".") MsgBox("The length is " & information.Length & ".") 

Despite the fact that it is documented and substantiated, this is a strange behavior. As a curiosity, I would also try to use this code below to see if the problem is also. Try using the Windows API and see if it returns cached data.

 Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile _ As Long, lpCreationTime As FILETIME, lpLastAccessTime _ As FILETIME, lpLastWriteTime As FILETIME) As Long 

If this does not solve, try to program the "Open Properties" window, then read the date information and see if it solves the problem:

 '#VBIDEUtils#*********************************************** ' * Programmer Name : Waty Thierry ' * Web Site : www.geocities.com/ResearchTriangle/6311/ ' * E-Mail : waty.thierry@usa.net ' * Date : 28/06/99 ' * Time : 13:16 ' ******************************************************** ' * Comments : Showing the Properties dialog box ' * ' * This tip demonstrates how To use the Win32 API To ' * bring up the explorer properties ' * dialog box For a specified file. ' * This API Function has the same effect As Right ' * clicking On a file In Windows 95 And selecting properties. ' ************************************************** Option Explicit On Private Type SHELLEXECUTEINFO cbSize As Long fMask As Long hwnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long lpIDList As Long lpClass As String hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End Type Private Const SEE_MASK_INVOKEIDLIST = &HC Private Const SEE_MASK_NOCLOSEPROCESS = &H40 Private Const SEE_MASK_FLAG_NO_UI = &H400 Private Declare Function ShellExecuteEX Lib "shell32.dll" Alias _ "ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long Public Function ShowProps(FileName As String, _ OwnerhWnd As Long) As Boolean 'USAGE: 'To show the properties dialog box of "c:\autoexec.bat", use the following code: 'Call ShowProps("c:\autoexec.bat", Me.hwnd) 'Function will return false if 'property windows can't be shown for 'any reason (eg, invalid file or Ownerhwnd) On Error Resume Next Dim SEI As SHELLEXECUTEINFO Dim r As Long With SEI .cbSize = Len(SEI) .fMask = SEE_MASK_NOCLOSEPROCESS Or _ SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI .hwnd = OwnerhWnd .lpVerb = "properties" .lpFile = FileName .lpParameters = vbNullChar .lpDirectory = vbNullChar .nShow = 0 .hInstApp = 0 .lpIDList = 0 End With r = ShellExecuteEX(SEI) ShowProps = r End Function 
0
source

Source: https://habr.com/ru/post/1502604/


All Articles