File.SetLastWriteTime(String, DateTime) メソッド

定義

指定したファイルに最後に書き込んだ日付と時刻を設定します。Sets the date and time that the specified file was last written to.

public:
 static void SetLastWriteTime(System::String ^ path, DateTime lastWriteTime);
public static void SetLastWriteTime (string path, DateTime lastWriteTime);
static member SetLastWriteTime : string * DateTime -> unit
Public Shared Sub SetLastWriteTime (path As String, lastWriteTime As DateTime)

パラメーター

path
String

日時情報を設定する対象のファイル。The file for which to set the date and time information.

lastWriteTime
DateTime

path に最後に書き込んだ日付と時刻の設定値を含む DateTimeA DateTime containing the value to set for the last write date and time of path. この値は現地時刻で表示されます。This value is expressed in local time.

例外

path は長さ 0 の文字列であるか、空白のみで構成されるか、または InvalidPathChars で定義される 1 つ以上の使用できない文字を含んでいます。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

pathnullです。path is null.

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。The specified path, file name, or both exceed the system-defined maximum length.

指定したパスが見つかりませんでした。The specified path was not found.

呼び出し元に、必要なアクセス許可がありません。The caller does not have the required permission.

path の形式が正しくありません。path is in an invalid format.

lastWriteTime はこの操作に許可される日付または時刻の範囲外の値を指定しています。lastWriteTime specifies a value outside the range of dates or times permitted for this operation.

次の例では、指定されたファイルのファイルシステムをチェックし、必要に応じてファイルを作成して、ファイルの最終書き込み時刻を設定して取得します。The following example checks the file system for the specified file, creating the file if necessary, and then sets and gets the last write time of the file.

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());
        }
    }
}
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 an 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パラメーターで相対パスまたは絶対パス情報を指定できます。The path parameter is permitted to specify relative or absolute path information. 相対パス情報は、現在の作業ディレクトリを基準として解釈されます。Relative path information is interpreted as relative to the current working directory. 現在の作業ディレクトリを取得するには、「」を参照してください GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。For a list of common I/O tasks, see Common I/O Tasks.

適用対象

こちらもご覧ください