File.SetLastWriteTime 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
SetLastWriteTime(SafeFileHandle, DateTime) |
設定上次寫入指定檔案或目錄的日期和時間。 |
SetLastWriteTime(String, DateTime) |
設定指定檔案上次被寫入的日期和時間。 |
SetLastWriteTime(SafeFileHandle, DateTime)
- 來源:
- 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
要 SafeFileHandle 設定上次寫入日期與時間資訊的檔案或目錄的 。
例外狀況
fileHandle
為 null
。
lastWriteTime
指定的值超出這項作業允許的日期或時間範圍 (或兩者皆超出)。
呼叫端沒有必要的權限。
執行此作業時發生 I/O 錯誤
適用於
SetLastWriteTime(String, DateTime)
- 來源:
- 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 和 2.1 之前的 .NET Core 版本:path
是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。
path
為 null
。
指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。
找不到指定的路徑。
呼叫端沒有必要的權限。
path
格式無效。
lastWriteTime
指定的值超出這項作業允許的日期或時間範圍。
範例
下列範例會檢查文件系統是否有指定的檔案、視需要建立檔案,然後設定並取得檔案的上次寫入時間。
using namespace System;
using namespace System::IO;
int 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, 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 );
}
}
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 工作的清單,請參閱 一般 I/O 工作。