File.SetLastAccessTime(String, DateTime) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したファイルが最後にアクセスされた日時を設定します。
public:
static void SetLastAccessTime(System::String ^ path, DateTime lastAccessTime);
public static void SetLastAccessTime(string path, DateTime lastAccessTime);
static member SetLastAccessTime : string * DateTime -> unit
Public Shared Sub SetLastAccessTime (path As String, lastAccessTime As DateTime)
パラメーター
- path
- String
アクセス日時情報を設定する対象のファイル。
例外
2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。
GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。
path は nullです。
指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。
指定したパスが見つかりませんでした。
呼び出し元に必要なアクセス許可がありません。
path が無効な形式です。
lastAccessTime は、この操作で許可されている日付または時刻の範囲外の値を指定します。
例
次の例では、指定したファイルのファイル システムをチェックし、必要に応じてファイルを作成し、最後のアクセス時刻を設定して取得します。
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);
// Update the last access time.
}
File.SetLastAccessTime(path, new DateTime(1985,5,4));
// Get the creation time of a well-known directory.
DateTime dt = File.GetLastAccessTime(path);
Console.WriteLine("The last access time for this file was {0}.", dt);
// Update the last access time.
File.SetLastAccessTime(path, DateTime.Now);
dt = File.GetLastAccessTime(path);
Console.WriteLine("The last access time for this file was {0}.", dt);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
open System
open System.IO
try
let path = @"c:\Temp\MyTest.txt"
if File.Exists path |> not then
File.Create path |> ignore
// Update the last access time.
File.SetLastAccessTime(path, DateTime(1985, 5, 4))
// Get the creation time of a well-known directory.
let dt = File.GetLastAccessTime path
printfn $"The last access time for this file was {dt}."
// Update the last access time.
File.SetLastAccessTime(path, DateTime.Now)
let dt = File.GetLastAccessTime path
printfn $"The last access time for this file was {dt}."
with
| e -> printfn $"The process failed: {e}"
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)
End If
File.SetLastAccessTime(path, New DateTime(1985, 5, 4))
' Get the creation time of a well-known directory.
Dim dt As DateTime = File.GetLastAccessTime(path)
Console.WriteLine("The last access time for this file was {0}.", dt)
' Update the last access time.
File.SetLastAccessTime(path, DateTime.Now)
dt = File.GetLastAccessTime(path)
Console.WriteLine("The last access 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 パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。
一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。