获取文件的创建日期或修改日期?
Get date creation or date modified of a file?
我想获取创建日期、最后修改日期以及与文件相关的其他详细信息,并将它们添加到数据网格视图中。目前我正在使用 directory.getfiles
获取文件信息。
这是我到目前为止得到的:
Dim paths() As String = IO.Directory.GetFiles("mypath")
For Each sFile As String In paths
Dim fileNameOnly As String = Path.GetFileNameWithoutExtension(sFile)
gridview.Rows.Add(fileNameOnly)
Next
如果您使用 DirectoryInfo
对象获取文件列表,您将可以访问更多数据:
Dim di As DirectoryInfo = New DirectoryInfo("mypath")
那么你的循环看起来更像:
For Each fi In di.GetFiles("*", SearchOption.AllDirectories)
Dim fileNameOnly As String = fi.Name
Dim createDate as Date = fi.CreationTime
<etc...>
Next
有关 FileInfo
的完整说明,请参见此处:
*我的vb可能生锈了
我在借this function。通过使用该信息填充数据表,它变得非常简单,您可以在需要时进一步操作。
gridview.DataSource = Fileinfo_To_DataTable("mypath")
Private Function Fileinfo_To_DataTable(ByVal directory As String) As DataTable
Try
'Create a new data table
Dim dt As DataTable = New DataTable
'Add the following columns: Name. Length Last Write Time, Creation Time
dt.Columns.Addrange({New DataColumn("Name"), New DataColumn("Length", GetType(Long)), New DataColumn("Last Write Time", GetType(Date)), New DataColumn("Creation Time", GetType(Date))})
'Loop through each file in the directory
For Each file As IO.FileInfo In New IO.DirectoryInfo(directory).GetFiles
'Create a new row
Dim dr As DataRow = dt.NewRow
'Set the data
dr(0) = file.Name
dr(1) = file.Length
dr(2) = file.LastWriteTime
dr(3) = file.CreationTime
'Add the row to the data table
dt.Rows.Add(dr)
Next
'Return the data table
Return dt
Catch ex As Exception
Console.WriteLine(ex.ToString)
'Return nothing if something fails
Return Nothing
End Try
End Function
这就是我获取创建日期和最后写入时间的方式。
For each sfile as datetime.tostring in paths
Dim fileCreatedDate As DateTime = File.GetCreationTime(paths)
Dim fileLastWrite as DateTime = File.GetLastWriteTime(path)
Next
要获取某个日期之间的文件,试试这个..
Dim [date] As DateTime = firstdatevariable
While [date] <= seconddatevariable
'add dates that are inbetween them
End While
在我看来最简单的就是这个例子。
我在我的文档
中寻找一个名为Symbols.csv的文件
Friend ReadOnly MyDocsFolder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim SymbolsFile As String = Path.Combine(MyDocsFolder, "Symbols.csv")
Dim dt As Date = File.GetLastWriteTime(SymbolsFile)
Text = "Data last saved at: " & dt.ToShortDateString
我想获取创建日期、最后修改日期以及与文件相关的其他详细信息,并将它们添加到数据网格视图中。目前我正在使用 directory.getfiles
获取文件信息。
这是我到目前为止得到的:
Dim paths() As String = IO.Directory.GetFiles("mypath")
For Each sFile As String In paths
Dim fileNameOnly As String = Path.GetFileNameWithoutExtension(sFile)
gridview.Rows.Add(fileNameOnly)
Next
如果您使用 DirectoryInfo
对象获取文件列表,您将可以访问更多数据:
Dim di As DirectoryInfo = New DirectoryInfo("mypath")
那么你的循环看起来更像:
For Each fi In di.GetFiles("*", SearchOption.AllDirectories)
Dim fileNameOnly As String = fi.Name
Dim createDate as Date = fi.CreationTime
<etc...>
Next
有关 FileInfo
的完整说明,请参见此处:
*我的vb可能生锈了
我在借this function。通过使用该信息填充数据表,它变得非常简单,您可以在需要时进一步操作。
gridview.DataSource = Fileinfo_To_DataTable("mypath")
Private Function Fileinfo_To_DataTable(ByVal directory As String) As DataTable
Try
'Create a new data table
Dim dt As DataTable = New DataTable
'Add the following columns: Name. Length Last Write Time, Creation Time
dt.Columns.Addrange({New DataColumn("Name"), New DataColumn("Length", GetType(Long)), New DataColumn("Last Write Time", GetType(Date)), New DataColumn("Creation Time", GetType(Date))})
'Loop through each file in the directory
For Each file As IO.FileInfo In New IO.DirectoryInfo(directory).GetFiles
'Create a new row
Dim dr As DataRow = dt.NewRow
'Set the data
dr(0) = file.Name
dr(1) = file.Length
dr(2) = file.LastWriteTime
dr(3) = file.CreationTime
'Add the row to the data table
dt.Rows.Add(dr)
Next
'Return the data table
Return dt
Catch ex As Exception
Console.WriteLine(ex.ToString)
'Return nothing if something fails
Return Nothing
End Try
End Function
这就是我获取创建日期和最后写入时间的方式。
For each sfile as datetime.tostring in paths
Dim fileCreatedDate As DateTime = File.GetCreationTime(paths)
Dim fileLastWrite as DateTime = File.GetLastWriteTime(path)
Next
要获取某个日期之间的文件,试试这个..
Dim [date] As DateTime = firstdatevariable
While [date] <= seconddatevariable
'add dates that are inbetween them
End While
在我看来最简单的就是这个例子。 我在我的文档
中寻找一个名为Symbols.csv的文件Friend ReadOnly MyDocsFolder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim SymbolsFile As String = Path.Combine(MyDocsFolder, "Symbols.csv")
Dim dt As Date = File.GetLastWriteTime(SymbolsFile)
Text = "Data last saved at: " & dt.ToShortDateString