简体   繁体   中英

Get date creation or date modified of a file?

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:

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

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

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 :

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

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

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

Simplest in my view would be something like this example. I here look for a file called Symbols.csv in My Documents

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM