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
目录的路径。
返回
一个设置为指定目录的创建日期和时间的结构。 该值用本地时间表示。
例外
调用方没有所要求的权限。
.NET Framework 和 .NET Core 版本早于 2.1: path
是零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
path
为 null
。
指定的路径和/或文件名超过了系统定义的最大长度。
示例
以下示例获取指定目录的创建时间。
using namespace System;
using namespace System::IO;
int 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 );
}
}
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
注解
注意
此方法可能返回不准确的值,因为它使用本机函数,其值可能不会由操作系统持续更新。
此方法等效于 File.GetCreationTime。
如果 参数中描述的 path
目录不存在,则此方法返回 1601 年 1 月 1 日午夜 12:00, (C.E.) 协调世界时 (UTC) ,调整为本地时间。
允许 path
参数指定相对路径信息或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
参数的 path
区分大小写对应于运行代码的文件系统的区分大小写。 例如,它在 NTFS 上不区分大小写, (默认 Windows 文件系统) ,在 Linux 文件系统上区分大小写。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。