【问题标题】:GetLastWriteTime not accurateGetLastWriteTime 不准确
【发布时间】:2013-09-21 21:20:03
【问题描述】:

我正在使用一个函数来检查远程计算机上文件的时间戳。我们有很多电脑。

我遇到的有点奇怪。

我在很多计算机上运行查询。 Computer26887 上的时间戳显示为“1/4/2013 12:46:01 AM” 我 UNC 到那台计算机并检查文件。在资源管理器中,时间戳显示“9/16/2013 11:23 AM”(一个半小时前)嗯...... 再次查询 - 相同的旧的、错误的时间戳。我右键单击该文件,转到属性并单击属性窗口中的“详细信息”-“修改日期 2013 年 9 月 16 日上午 11:23” 使用 vb 代码再次查询 - 现在它显示正确的时间戳????

我有数百个系统要检查,如果我不能相信我得到的数据,我有太多的工作要做!!!

有什么想法吗?

更新
基本上,VB.NET 正在检索时间戳的缓存版本。时间戳已更新,但缓存中仍有旧的时间戳。如何强制更新缓存而不在资源管理器中手动打开文件的属性?

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

(我也尝试过使用File 而不是FileInfo...查看帖子历史记录)

更新
作为测试,我使用 AutoIT3 代码对系统进行了文件检查。它返回了准确的信息。在 AutoIT3 检查之后,vb.net 返回了准确的时间戳。那么,vb.net 的 AutoIT3 做得更好的问题是什么?

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

【问题讨论】:

  • 这听起来很奇怪。有没有可能发生时区不同的事情?顺便说一句,作为一个建议,您可能需要考虑使用MessageBox.Show 而不是MsgBoxRet.Replace 而不是Replace,或者将info 设为Enum,或者将方法分解为每个Case 都有一个单独的方法。
  • 这很正常,文件系统没有义务在进程打开文件时不断更新属性。在该属性的 MSDN 文章中有一个响亮的注释。您唯一可以保证的是,当所有进程都关闭文件的句柄时它是准确的。您将很难从远程机器上看到的东西。
  • @HansPassant - 这是没有来自 vb .net 准确时间戳的日子 - 在资源管理器等中准确。这些文件不会经常使用或打开。时间戳应至少在 30 分钟内可用。已经有几天了 - 我可以“强制”在资源管理器和任何 vb .net 正在查看的内容之间进行同步,如上所述。
  • 您是否尝试过使用 FileInfo 对象而不是 File 方法?如果它最初不起作用,您可以在前者上调用 .Refresh() 。您是否考虑过为"\\" & ComputerName & "\" & FiletoFind 使用变量而不是多次输入?请注意,您的 <date>.ToString() 结果将取决于运行它的计算机上的日期设置 - 您可能想要使用 .ToString("MM/dd/yyyy hh:mm:ss tt")
  • 我必须同意@AndrewMorton。文档实际上声明 Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated. 使用 FileSystemInfo 而不是 File

标签: .net vb.net file-io


【解决方案1】:

试试 Andrew Morton 和 neoistheone 指出的 FileSystem.GetFileInfo

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's full name is " & information.FullName & ".")
MsgBox("Last access time is " & information.LastAccessTime & ".")
MsgBox("The length is " & information.Length & ".")

尽管有记录和理由,但这是一种奇怪的行为。 出于好奇,我也会尝试下面的这些代码,看看问题是否也会发生。 尝试使用 Windows API 并查看它是否仍然返回“缓存”数据。

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

如果这不能解决,请尝试以编程方式调用“打开属性”窗口,然后读取日期时间信息,看看是否可以解决问题:

'#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 (e.g., 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 2023-03-16
    • 1970-01-01
    • 2021-07-28
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多