Directory.SetLastWriteTime(String, DateTime) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Anger datum och tid som en katalog senast skrevs till.
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)
Parametrar
- path
- String
Sökvägen till katalogen.
- lastWriteTime
- DateTime
Datum och tid som katalogen senast skrevs till. Det här värdet uttrycks i lokal tid.
Undantag
path hittades inte (till exempel finns inte katalogen eller så finns den på en ommappad enhet).
path hittades inte (till exempel finns inte katalogen eller så finns den på en ommappad enhet).
.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 GetInvalidPathChars() metoden.
path är null.
Den angivna sökvägen, filnamnet eller båda överskrider den systemdefinierade maximala längden.
Anroparen har inte den behörighet som krävs.
Det aktuella operativsystemet är inte Windows NT eller senare.
lastWriteTime 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 visas hur du använder SetLastWriteTime.
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
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.
Parameterns path skiftlägeskänslighet motsvarar det filsystem där koden körs. Det är till exempel skiftlägeskänsligt på NTFS (standard Windows filsystem) och skiftlägeskänsligt på Linux-filsystem.
En lista över vanliga I/O-uppgifter finns i Vanliga I/O-uppgifter.