Hi samsmuthu,
You are getting default time because the file does not exist.
As per documentation File.GetCreationTime(String) 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.
So before you can check the file exist or not then proceed.
Check below code.
There are two way to get the CreationTime.
Using File Class
C#
string filePath = Server.MapPath("~/Test.txt");
if (System.IO.File.Exists(filePath))
{
DateTime createdTime = System.IO.File.GetCreationTime(filePath);
DateTime modifiedDate = System.IO.File.GetLastWriteTime(filePath);
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Created Date : " + createdTime.ToString() + "\\nModified Date : " + modifiedDate + "');", true);
}
VB.Net
Dim filePath As String = Server.MapPath("~/Test.txt")
If System.IO.File.Exists(filePath) Then
Dim createdTime As DateTime = System.IO.File.GetCreationTime(filePath)
Dim modifiedDate As DateTime = System.IO.File.GetLastWriteTime(filePath)
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('Created Date : " & createdTime.ToString() & "\nModified Date : " + modifiedDate & "');", True)
End If
Using FileInfo Class
C#
string filePath = Server.MapPath("~/Test.txt");
if (System.IO.File.Exists(filePath))
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
DateTime createdTime = fileInfo.CreationTime;
DateTime modifiedDate = fileInfo.LastWriteTime;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Created Date : " + createdTime.ToString() + "\\nModified Date : " + modifiedDate + "');", true);
}
VB.Net
Dim filePath As String = Server.MapPath("~/Test.txt")
If System.IO.File.Exists(filePath) Then
Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(filePath)
Dim createdTime As DateTime = fileInfo.CreationTime
Dim modifiedDate As DateTime = fileInfo.LastWriteTime
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('Created Date : " & createdTime.ToString() & "\nModified Date : " + modifiedDate & "');", True)
End If
Screenshot