File.SetLastAccessTime(String, DateTime) Metod

Definition

Anger datum och tid då den angivna filen senast användes.

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)

Parametrar

path
String

Filen som du vill ange information om åtkomstdatum och tid för.

lastAccessTime
DateTime

En DateTime som innehåller värdet som ska anges för det senaste åtkomstdatumet och -tiden pathför . Det här värdet uttrycks i lokal tid.

Undantag

.NET Framework- och .NET Core-versioner som är äldre än 2.1: path är en sträng med noll längd, innehåller endast tomt utrymme eller innehåller ett eller flera ogiltiga tecken. Du kan fråga efter ogiltiga tecken med hjälp GetInvalidPathChars() av metoden .

path är null.

Den angivna sökvägen, filnamnet eller båda överskrider den systemdefinierade maximala längden.

Det gick inte att hitta den angivna sökvägen.

Anroparen har inte den behörighet som krävs.

path är i ett ogiltigt format.

lastAccessTime anger ett värde utanför det datum- eller tidsintervall som tillåts för den här åtgärden.

Exempel

I följande exempel kontrolleras filsystemet för den angivna filen, skapar den om det behövs och anger och hämtar sedan den senaste åtkomsttiden.

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

Kommentarer

Parametern path tillåts ange relativ eller absolut sökvägsinformation. Relativ sökvägsinformation tolkas som relativ till den aktuella arbetskatalogen. Information om hur du hämtar den aktuella arbetskatalogen finns i GetCurrentDirectory.

En lista över vanliga I/O-uppgifter finns i Vanliga I/O-uppgifter.

Gäller för

Se även