File.SetLastAccessTime(String, DateTime) Methode

Definitie

Hiermee stelt u de datum en tijd in waarop het opgegeven bestand voor het laatst is geopend.

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)

Parameters

path
String

Het bestand waarvoor de toegangsdatum- en tijdgegevens moeten worden ingesteld.

lastAccessTime
DateTime

Een DateTime met de waarde die moet worden ingesteld voor de datum en tijd van pathde laatste toegang. Deze waarde wordt uitgedrukt in lokale tijd.

Uitzonderingen

.NET Framework en .NET Core-versies ouder dan 2.1: path is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens. U kunt een query uitvoeren op ongeldige tekens met behulp van de GetInvalidPathChars() methode.

path is null.

Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.

Het opgegeven pad is niet gevonden.

De beller heeft niet de vereiste machtiging.

path heeft een ongeldige indeling.

lastAccessTime geeft een waarde buiten het bereik van datums of tijden die zijn toegestaan voor deze bewerking.

Voorbeelden

In het volgende voorbeeld wordt het bestandssysteem gecontroleerd op het opgegeven bestand, wordt het zo nodig gemaakt en vervolgens ingesteld en wordt de laatste toegangstijd ingesteld.

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

Opmerkingen

De path parameter mag relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap. Als u de huidige werkmap wilt ophalen, raadpleegt GetCurrentDirectoryu .

Zie Algemene I/O-taken voor een lijst met algemene I/O-taken.

Van toepassing op

Zie ook