File.GetLastWriteTime 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
GetLastWriteTime(String) |
傳回指定檔案或目錄上次被寫入的日期和時間。 |
GetLastWriteTime(SafeFileHandle) |
傳回指定之檔案或目錄的最後寫入日期和時間。 |
GetLastWriteTime(String)
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
傳回指定檔案或目錄上次被寫入的日期和時間。
public:
static DateTime GetLastWriteTime(System::String ^ path);
public static DateTime GetLastWriteTime (string path);
static member GetLastWriteTime : string -> DateTime
Public Shared Function GetLastWriteTime (path As String) As DateTime
參數
- path
- String
要取得其寫入日期和時間資訊的檔案或目錄。
傳回
DateTime 結構,設定為指定檔案或目錄的上次寫入日期和時間。 這個值以本地時間表示。
例外狀況
呼叫端沒有必要的權限。
.NET Framework 和 2.1 之前的 .NET Core 版本:path
是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。
path
為 null
。
指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。
path
格式無效。
範例
下列範例示範 GetLastWriteTime
。
using namespace System;
using namespace System::IO;
int main()
{
try
{
String^ path = "c:\\Temp\\MyTest.txt";
if ( !File::Exists( path ) )
{
File::Create( path );
}
else
{
// Take an action that will affect the write time.
File::SetLastWriteTime( path, DateTime(1985,4,3) );
}
// Get the creation time of a well-known directory.
DateTime dt = File::GetLastWriteTime( path );
Console::WriteLine( "The last write time for this file was {0}.", dt );
// Update the last write time.
File::SetLastWriteTime( path, DateTime::Now );
dt = File::GetLastWriteTime( path );
Console::WriteLine( "The last write time for this file was {0}.", dt );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
string path = @"c:\Temp\MyTest.txt";
if (!File.Exists(path))
{
File.Create(path);
}
else
{
// Take an action that will affect the write time.
File.SetLastWriteTime(path, new DateTime(1985,4,3));
}
// Get the creation time of a well-known directory.
DateTime dt = File.GetLastWriteTime(path);
Console.WriteLine("The last write time for this file was {0}.", dt);
// Update the last write time.
File.SetLastWriteTime(path, DateTime.Now);
dt = File.GetLastWriteTime(path);
Console.WriteLine("The last write time for this file was {0}.", dt);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
open System
open System.IO
let path = @"c:\Temp\MyTest.txt"
if File.Exists path |> not then
File.Create path |> ignore
else
// Take an action that will affect the write time.
File.SetLastWriteTime(path, DateTime(1985, 4, 3))
// Get the creation time of a well-known directory.
let dt = File.GetLastWriteTime path
printfn $"The last write time for this file was {dt}."
// Update the last write time.
File.SetLastWriteTime(path, DateTime.Now)
let dt2 = File.GetLastWriteTime path
printfn $"The last write time for this file was {dt2}."
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Try
Dim path As String = "c:\Temp\MyTest.txt"
If File.Exists(path) = False Then
File.Create(path)
Else
' Take some action that will affect the write time.
File.SetLastWriteTime(path, New DateTime(1985, 4, 3))
End If
'Get the creation time of a well-known directory.
Dim dt As DateTime = File.GetLastWriteTime(path)
Console.WriteLine("The last write time for this file was {0}.", dt)
'Update the last write time.
File.SetLastWriteTime(path, DateTime.Now)
dt = File.GetLastWriteTime(path)
Console.WriteLine("The last write time for this file was {0}.", dt)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
備註
注意
這個方法可能會傳回不正確的值,因為它會使用操作系統可能不會持續更新其值的原生函式。 每個作業系統都會根據自己的規則來管理上次寫入時間。 為了改善效能,操作系統可能不會將上次寫入時間值設定為上次寫入作業的確切時間,但可能會改為將它設定為接近的近似值。
如果 參數中所述的 path
檔案不存在,此方法會傳回 12:00 A.1601 A.D 的午夜, (C.E.) 國際標準時間 (UTC) ,調整為當地時間。
允許 path
參數指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory。
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。
另請參閱
適用於
GetLastWriteTime(SafeFileHandle)
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
傳回指定之檔案或目錄的最後寫入日期和時間。
public:
static DateTime GetLastWriteTime(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle);
public static DateTime GetLastWriteTime (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle);
static member GetLastWriteTime : Microsoft.Win32.SafeHandles.SafeFileHandle -> DateTime
Public Shared Function GetLastWriteTime (fileHandle As SafeFileHandle) As DateTime
參數
- fileHandle
- SafeFileHandle
SafeFileHandle要取得上次寫入日期與時間資訊的檔案或目錄的 。
傳回
結構 DateTime 會設定為指定之檔案或目錄的上次寫入日期和時間。 這個值以本地時間表示。
例外狀況
fileHandle
為 null
。
呼叫端沒有必要的權限。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應