File.GetCreationTime(String) 方法

定义

返回指定文件或目录的创建日期和时间。Returns the creation date and time of the specified file or directory.

public:
 static DateTime GetCreationTime(System::String ^ path);
public static DateTime GetCreationTime (string path);
static member GetCreationTime : string -> DateTime
Public Shared Function GetCreationTime (path As String) As DateTime

参数

path
String

要获取其创建日期和时间信息的文件或目录。The file or directory for which to obtain creation date and time information.

返回

DateTime

一个 DateTime 结构,它被设置为指定文件或目录的创建日期和时间。A DateTime structure set to the creation date and time for the specified file or directory. 该值用本地时间表示。This value is expressed in local time.

例外

调用方没有所要求的权限。The caller does not have the required permission.

path 是一个长度为零的字符串,仅包含空格,或包含一个或多个由 InvalidPathChars 定义的无效字符。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

pathnullpath is null.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.

path 的格式无效。path is in an invalid format.

示例

下面的示例演示了 GetCreationTimeThe following example demonstrates GetCreationTime.

Imports System.IO

Module Module1

    Sub Main()
        Dim fileCreatedDate As DateTime = File.GetCreationTime("C:\Example\MyTest.txt")
        Console.WriteLine("file created: " + fileCreatedDate)
    End Sub

End Module

注解

备注

此方法可能会返回不准确的值,因为它使用的本机函数的值可能不会持续由操作系统进行更新。This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system.

path允许参数指定相对或绝对路径信息。The path parameter is permitted to specify relative or absolute path information. 相对路径信息被解释为相对于当前工作目录。Relative path information is interpreted as relative to the current working directory. 若要获取当前工作目录,请参见 GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

如果参数中所述的文件 path 不存在,此方法将返回12:00 年1月1日午夜1601If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (公历 ) 协调世界时 (UTC) ,并调整为本地时间。(C.E.) Coordinated Universal Time (UTC), adjusted to local time.

NTFS 格式的驱动器可能会在短时间内缓存有关文件的信息,例如文件创建时间。NTFS-formatted drives may cache information about a file, such as file creation time, for a short period of time. 因此,如果要覆盖或替换现有文件,可能需要显式设置文件的创建时间。As a result, it may be necessary to explicitly set the creation time of a file if you are overwriting or replacing an existing file.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于

另请参阅