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

定義

ディレクトリに最後に書き込んだ日付と時刻を設定します。

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

ディレクトリのパス。

lastWriteTime
DateTime

ディレクトリに最後に書き込んだ日付と時刻。 この値は現地時刻で表示されます。

例外

path が見つかりませんでした (たとえば、ディレクトリが存在しない、またはマップされていないドライブにあるなど)。

path が見つかりませんでした (たとえば、ディレクトリが存在しない、またはマップされていないドライブにあるなど)。

.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: path は長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 GetInvalidPathChars() メソッドを使用して、正しくない文字に対するクエリを実行できます。

pathnullです。

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

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

現在のオペレーティング システムは Windows NT 以降ではありません。

lastWriteTime はこの操作に許可される日付または時刻の範囲外の値を指定しています。

次の例は、SetLastWriteTime の使用方法を示しています。

using namespace System;
using namespace System::IO;
void main()
{
   try
   {
      String^ path = "c:\\MyDir";
      if (  !Directory::Exists( path ) )
      {
         Directory::CreateDirectory( path );
      }
      else
      {
         
         // Take an action that will affect the write time.
         Directory::SetLastWriteTime( path, DateTime(1985,4,3) );
      }
      
      // Get the last write time of a well-known directory.
      DateTime dt = Directory::GetLastWriteTime( path );
      Console::WriteLine( "The last write time for this directory was {0}", dt );
      
      //Update the last write time.
      Directory::SetLastWriteTime( path, DateTime::Now );
      dt = Directory::GetLastWriteTime( path );
      Console::WriteLine( "The last write time for this directory 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:\MyDir";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            else
            {
                // Take an action that will affect the write time.
                Directory.SetLastWriteTime(path, new DateTime(1985,4,3));
            }

            // Get the last write time of a well-known directory.
            DateTime dt = Directory.GetLastWriteTime(path);
            Console.WriteLine("The last write time for this directory was {0}", dt);
            
            //Update the last write time.
            Directory.SetLastWriteTime(path, DateTime.Now);
            dt = Directory.GetLastWriteTime(path);
            Console.WriteLine("The last write time for this directory was {0}", dt);
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
open System
open System.IO

try
    let path = @"c:\MyDir"
    if not (Directory.Exists path) then
        Directory.CreateDirectory path |> ignore

    else
        // Take an action that will affect the write time.
        Directory.SetLastWriteTime(path, DateTime(1985, 4, 3))

    // Get the last write time of a well-known directory.
    let dt = Directory.GetLastWriteTime path
    printfn $"The last write time for this directory was {dt}"
    
    //Update the last write time.
    Directory.SetLastWriteTime(path, DateTime.Now)
    let dt = Directory.GetLastWriteTime path
    printfn $"The last write time for this directory was {dt}"
with e ->
    printfn $"The process failed: {e}"
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            Dim path As String = "c:\MyDir"
            If Directory.Exists(path) = False Then
                Directory.CreateDirectory(path)
            Else
                ' Take an action that will affect the write time.
                Directory.SetLastWriteTime(path, New DateTime(1985, 4, 3))
            End If

            'Get the last write time of a well-known directory.
            Dim dt As DateTime = Directory.GetLastWriteTime(path)
            Console.WriteLine("The last write time for this directory was {0}", dt)

            'Update the last write time.
            Directory.SetLastWriteTime(path, DateTime.Now)
            dt = Directory.GetLastWriteTime(path)
            Console.WriteLine("The last write time for this directory was {0}", dt)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

注釈

パラメーターは path 、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、「」を参照してください GetCurrentDirectory

パラメーターの大文字と小文字の path 区別は、コードが実行されているファイル システムの大文字と小文字が区別されます。 たとえば、NTFS (既定の Windows ファイル システム) では大文字と小文字が区別されず、Linux ファイル システムでは大文字と小文字が区別されます。

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

適用対象

こちらもご覧ください