File.GetLastWriteTime メソッド

定義

オーバーロード

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 構造体。 この値は現地時刻で表示されます。

例外

呼び出し元に、必要なアクセス許可がありません。

2.1 より前のバージョンの.NET Frameworkと .NET Core: path は長さ 0 の文字列、空白のみを含む、または 1 つ以上の無効な文字を含みます。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。

pathnullです。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

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 日午前 0 時 00 分 (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指定したファイルまたはディレクトリの最後の書き込み日時に設定された構造体。 この値は現地時刻で表示されます。

例外

fileHandlenullです。

呼び出し元に、必要なアクセス許可がありません。

適用対象