vb.net 获取文件的创建日期或修改日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38751205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:00:22  来源:igfitidea点击:

Get date creation or date modified of a file?

vb.netdatagridviewsystem.io.directory

提问by TonyW

I want to get creation date, last modify date other details relating to files, and adding them to a datagridview. Currently I am getting the file information by using directory.getfiles. Here's what i got so far:

我想获取创建日期、上次修改日期以及与文件相关的其他详细信息,并将它们添加到数据网格视图中。目前我正在使用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

回答by Charles Paske

If you use the DirectoryInfoobject to get your list of files you'll have access to more data:

如果您使用该DirectoryInfo对象来获取文件列表,您将可以访问更多数据:

Dim di As DirectoryInfo = New DirectoryInfo("mypath")

Then your loop would look more like:

那么你的循环看起来更像是:

For Each fi In di.GetFiles("*", SearchOption.AllDirectories)
    Dim fileNameOnly As String = fi.Name
    Dim createDate as Date = fi.CreationTime
    <etc...>
Next

See this for a full description of FileInfo:

有关以下内容的完整说明,请参见此处FileInfo

https://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

https://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

*my vb may be rusty

*我的 vb 可能生锈了

回答by TonyW

This is how I would grab both the created date, and the last write time.

这就是我获取创建日期和上次写入时间的方式。

For each sfile as datetime.tostring in paths
     Dim fileCreatedDate As DateTime = File.GetCreationTime(paths)
     Dim fileLastWrite as DateTime = File.GetLastWriteTime(path)
Next

To get files inbetween a date, try this..

要在日期之间获取文件,请尝试此操作..

Dim [date] As DateTime = firstdatevariable
While [date] <= seconddatevariable
     'add dates that are inbetween them
End While

回答by Jimmy Smith

I'm borrowing this function. It makes it really simple by filling a datatable, with that info, that you can further manipulate if needed.

我正在借用这个功能。通过使用该信息填充数据表,它变得非常简单,如果需要,您可以进一步操作。

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