Getting Date Time created of a file in asp.net gridview
I have a gridview that looks something like this:
<TemplateColumn>
<ItemTe开发者_JAVA技巧mplate>
<a href='<%#Eval("FilePathUrl")%>'>FileName</a>
</ItemTemplate>
<ItemTemplate>
<a href='<%#Eval(sysUtilities.GetFilePath("FilePathLocation") & "FilePathUrl")%>'>FileDateTime</a>
</ItemTemplate>
</TemplateColumn>
The file is located at sysUtilities.GetFilePath("FilePathLocation")
sysUtilities is a class in the App_Code folder
GetFilePath is methodI need help with the syntax
How can I display the file's date time created in the gridView (by that I mean what is the correct syntax for):<a href='<%#Eval(sysUtilities.GetFilePath("FilePathLocation") & "FilePathUrl")%>'>FileDateTime</a>
try this:
<ItemTemplate>
<a href='<%#Eval(sysUtilities.GetFilePath("FilePathLocation") & "FilePathUrl")%>'>
<%#System.IO.File.GetCreationTime(sysUtilities.GetFilePath("FilePathLocation") & Eval("FilePathUrl").ToString())%>
</a>
</ItemTemplate>
Take a look here: http://www.csharp-examples.net/file-creation-modification-time/ for several examples on how to retrieve the file time. You can either use the FileInfo class or the File class, both of them exposing the same properties for retrieving the creation date time
Try this.
Markup
<asp:TemplateColumn HeaderText="Created At">
<ItemTemplate>
<asp:Label ID="FileCreationTime" runat="server"
Text='<%# GetFileCreatedTime(Eval(FilePathLocation), Eval(FilePathUrl) ) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
Code-behind
Protected Function GetFileCreatedTime(location As Object, url As Object) As String
Dim path As String = sysUtilities.GetFilePath(location.ToString()) & url.ToString()
Dim fi1 As FileInfo = New FileInfo(path)
If fi1.Exists Then
Return fi1.CreationTime.ToString()
Else
Return ""
End If
End Function
On an unrelated side-note, you are using asp:DataGrid
and not .asp:GridView
精彩评论