如何在C#中获取文件的创建时间 [英] How to get the creation time of file in C#

查看:439
本文介绍了如何在C#中获取文件的创建时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:FileUpload ID="FileUpload1" runat="server" />
 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


 // this is a aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class we : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
       DateTime creationTime = File.GetCreationTime(FileUpload1.FileName);
        
        Label1.Text =creationTime.ToString() ; 
    }
}


我尝试了这个..但是我得到了所有文件的输出01-01-1601 05:30:00.那该怎么办?


I tried this ..but I got the output 01-01-1601 05:30:00 for all files. So what should I do?

推荐答案

尝试使用DateTime.FromFileTime方法,
Try using the DateTime.FromFileTime method, http://msdn.microsoft.com/en-us/library/system.datetime.fromfiletime.aspx[^]


您可以尝试使用System.IO命名空间的``FileInfo''类.
System.IO.FileInfo类具有CreationTime属性,该属性可能会对您有所帮助.
You could try with the ''FileInfo'' class of the System.IO namespace.
The System.IO.FileInfo class has the CreationTime property that might help you.


FILETIME零是1601年1月1日00:00,它已调整为您的本地时间,变为05:30. >
文档说
如果path参数中描述的文件不存在,则此方法将返回1601年1月1日午夜12:00 AD(CE),协调世界时(UTC),调整为本地时间.

您应该检查FileUpload1.FileName是否包含所需的路径,以及该文件是否实际存在.

艾伦.
FILETIME zero is 00:00 1 January 1601 which adjusted to your local time becomes 05:30.

The documentation says
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.

You should check if FileUpload1.FileName contains the path you expect it to and that the file is actually there.

Alan.


这篇关于如何在C#中获取文件的创建时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆