File.SetLastWriteTime 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| SetLastWriteTime(SafeFileHandle, DateTime) |
設定指定檔案或目錄最後寫入的日期與時間。 |
| SetLastWriteTime(String, DateTime) |
設定指定檔案最後寫入的日期和時間。 |
SetLastWriteTime(SafeFileHandle, DateTime)
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
設定指定檔案或目錄最後寫入的日期與時間。
public:
static void SetLastWriteTime(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle, DateTime lastWriteTime);
public static void SetLastWriteTime(Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, DateTime lastWriteTime);
static member SetLastWriteTime : Microsoft.Win32.SafeHandles.SafeFileHandle * DateTime -> unit
Public Shared Sub SetLastWriteTime (fileHandle As SafeFileHandle, lastWriteTime As DateTime)
參數
- fileHandle
- SafeFileHandle
A SafeFileHandle 傳送到設定最後寫入日期與時間資訊的檔案或目錄。
例外狀況
fileHandle 是 null。
lastWriteTime 指定一個超出此操作允許日期、時間或兩者範圍的值。
來電者沒有所需的權限。
執行作業時發生 I/O 錯誤。
適用於
SetLastWriteTime(String, DateTime)
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
- 來源:
- File.cs
設定指定檔案最後寫入的日期和時間。
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
用來設定日期和時間資訊的檔案。
例外狀況
.NET Framework 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。
path 是 null。
指定的路徑、檔名或兩者都超過系統定義的最大長度。
找不到指定的路徑。
來電者沒有所需的權限。
path 格式無效。
lastWriteTime 指定一個超出此操作允許日期或時間範圍的值。
範例
以下範例檢查指定檔案的檔案系統,必要時建立該檔案,然後設定並取得該檔案的最後寫入時間。
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);
}
else
{
// Take an action that will affect the write time.
File.SetLastWriteTime(path, new DateTime(1985,4,3));
}
// Get the creation time of a well-known directory.
DateTime dt = File.GetLastWriteTime(path);
Console.WriteLine("The last write time for this file was {0}.", dt);
// Update the last write time.
File.SetLastWriteTime(path, DateTime.Now);
dt = File.GetLastWriteTime(path);
Console.WriteLine("The last write 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
else
// Take an action that will affect the write time.
File.SetLastWriteTime(path, DateTime(1985, 4, 3))
// Get the creation time of a well-known directory.
let dt = File.GetLastWriteTime path
printfn $"The last write time for this file was {dt}."
// Update the last write time.
File.SetLastWriteTime(path, DateTime.Now)
let dt = File.GetLastWriteTime path
printfn $"The last write 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)
Else
' Take an action that will affect the write time.
File.SetLastWriteTime(path, New DateTime(1985, 4, 3))
End If
' Get the creation time of a well-known directory.
Dim dt As DateTime = File.GetLastWriteTime(path)
Console.WriteLine("The last write time for this file was {0}.", dt)
' Update the last write time.
File.SetLastWriteTime(path, DateTime.Now)
dt = File.GetLastWriteTime(path)
Console.WriteLine("The last write 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 任務的清單,請參見 Common I/O 任務。