共用方式為


File.GetLastWriteTime 方法

定義

多載

GetLastWriteTime(SafeFileHandle)

傳回指定之檔案或目錄的最後寫入日期和時間。

GetLastWriteTime(String)

傳回指定檔案或目錄上次寫入的日期和時間。

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

呼叫端沒有必要的許可權。

適用於

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 結構設定為上次寫入指定檔案或目錄的日期和時間。 此值會以當地時間表示。

例外狀況

呼叫端沒有必要的許可權。

比 2.1 舊的 .NET Framework 和 .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 參數中所述的檔案不存在,這個方法會傳回 1601 年 1 月 1 日 1601 年 1 月 1 日午夜 12:00 (C.E.)國際標準時間(UTC),調整為當地時間。

path 參數可以指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於