Directory.GetCreationTime(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取目录的创建日期和时间。
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
目录的路径。
返回
设置为指定目录的创建日期和时间的结构。 此值以本地时间表示。
例外
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。
path 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
示例
以下示例获取指定目录的创建时间。
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
// Get the creation time of a well-known directory.
DateTime dt = Directory.GetCreationTime(Environment.CurrentDirectory);
// Give feedback to the user.
if (DateTime.Now.Subtract(dt).TotalDays > 364)
{
Console.WriteLine("This directory is over a year old.");
}
else if (DateTime.Now.Subtract(dt).TotalDays > 30)
{
Console.WriteLine("This directory is over a month old.");
}
else if (DateTime.Now.Subtract(dt).TotalDays <= 1)
{
Console.WriteLine("This directory is less than a day old.");
}
else
{
Console.WriteLine("This directory was created on {0}", dt);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
open System
open System.IO
try
// Get the creation time of a well-known directory.
let dt = Directory.GetCreationTime Environment.CurrentDirectory
// Give feedback to the user.
if DateTime.Now.Subtract(dt).TotalDays > 364 then
printfn "This directory is over a year old."
elif DateTime.Now.Subtract(dt).TotalDays > 30 then
printfn "This directory is over a month old."
elif DateTime.Now.Subtract(dt).TotalDays <= 1 then
printfn "This directory is less than a day old."
else
printfn $"This directory was created on {dt}"
with e ->
printfn $"The process failed: {e}"
Imports System.IO
Public Class Test
Public Shared Sub Main()
Try
' Get the creation time of a well-known directory.
Dim dt As DateTime = Directory.GetCreationTime(Environment.CurrentDirectory)
' Give feedback to the user.
If DateTime.Now.Subtract(dt).TotalDays > 364 Then
Console.WriteLine("This directory is over a year old.")
ElseIf DateTime.Now.Subtract(dt).TotalDays > 30 Then
Console.WriteLine("This directory is over a month old.")
ElseIf DateTime.Now.Subtract(dt).TotalDays <= 1 Then
Console.WriteLine("This directory is less than a day old.")
Else
Console.WriteLine("This directory was created on {0}", dt)
End If
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
注解
Note
此方法可能会返回不准确的值,因为它使用本机函数,其值可能不会由操作系统持续更新。
此方法等效于 File.GetCreationTime.
如果参数中描述的 path 目录不存在,此方法返回 1601 年 1 月 1 日午夜 12:00(C.E.)协调世界时(UTC),调整为当地时间。
允许参数 path 指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。