Get file modified date in VB.NET

早过忘川 提交于 2019-12-04 17:13:25

问题


I have a number of files in a folder, and I need to get the last modified date. So I used

FDate = IO.File.GetLastWriteTime(FName)

It works fine with some files, but on others, I get a date of 1/1/1601. But when I check the files in Windows Explorer, all the dates look normal (recent). So, I'm guessing there are multiple file dates stored in the file system, and the ones .NET is seeing are not the ones Windows is seeing. How can I get exactly the date which appears as "date modified" in a file explorer window?

I tried some Visual Basic 6.0 API stuff, but that doesn't seem to work in .NET.


回答1:


From File.GetLastWriteTime Method:

If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

The file you are querying is probably missing.




回答2:


The query mentioned below will get the correct LastModifiedDate for all of the files contained in a folder.

    Dim strFilepath = ""  'Specify path details
    Dim directory As New System.IO.DirectoryInfo(strFilepath)
    Dim File As System.IO.FileInfo() = directory.GetFiles()
    Dim File1 As System.IO.FileInfo
    For Each File1 In File
        Dim strLastModified As String
        strLastModified = System.IO.File.GetLastWriteTime(strFilepath & "\" & File1.Name).ToShortDateString()
    Next


来源:https://stackoverflow.com/questions/3668340/get-file-modified-date-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!