File.Delete(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
刪除指定的檔案。
public:
static void Delete(System::String ^ path);
public static void Delete (string path);
static member Delete : string -> unit
Public Shared Sub Delete (path As String)
參數
- path
- String
要刪除的檔案的名稱。 不支援萬用字元。
例外狀況
.NET Framework 和 2.1 之前的 .NET Core 版本:path
是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。
path
為 null
。
指定的路徑無效 (例如,它位於未對應的磁碟機上)。
指定的檔案正在使用中。
-或-
檔案上有開啟的控制代碼,而且作業系統是 Windows XP 或之前的版本。 這個開啟的控制代碼可能是列舉目錄和檔案所產生的結果。 如需詳細資訊,請參閱如何:列舉目錄和檔案。
path
格式無效。
指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。
範例
下列範例會將檔案群組複製到 C:\archives\2008 備份資料夾,然後從源資料夾刪除它們。
string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";
try
{
string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
string[] txtList = Directory.GetFiles(sourceDir, "*.txt");
// Copy picture files.
foreach (string f in picList)
{
// Remove path from the file name.
string fName = f.Substring(sourceDir.Length + 1);
// Use the Path.Combine method to safely append the file name to the path.
// Will overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
}
// Copy text files.
foreach (string f in txtList)
{
// Remove path from the file name.
string fName = f.Substring(sourceDir.Length + 1);
try
{
// Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
}
// Catch exception if the file was already copied.
catch (IOException copyError)
{
Console.WriteLine(copyError.Message);
}
}
// Delete source files that were copied.
foreach (string f in txtList)
{
File.Delete(f);
}
foreach (string f in picList)
{
File.Delete(f);
}
}
catch (DirectoryNotFoundException dirNotFound)
{
Console.WriteLine(dirNotFound.Message);
}
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"
try
let picList = Directory.GetFiles(sourceDir, "*.jpg")
let txtList = Directory.GetFiles(sourceDir, "*.txt")
// Copy picture files.
for f in picList do
// Remove path from the file name.
let fName = f.Substring(sourceDir.Length + 1)
// Use the Path.Combine method to safely append the file name to the path.
// Will overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true)
// Copy text files.
for f in txtList do
// Remove path from the file name.
let fName = f.Substring(sourceDir.Length + 1)
try
// Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))
// Catch exception if the file was already copied.
with
| :? IOException as copyError -> printfn $"{copyError.Message}"
// Delete source files that were copied.
for f in txtList do
File.Delete f
for f in picList do
File.Delete f
// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
Dim sourceDir As String = "c:\current"
Dim backupDir As String = "c:\archives\2008"
Try
Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg")
Dim txtList As String() = Directory.GetFiles(sourceDir, "*.txt")
' Copy picture files.
For Each f As String In picList
'Remove path from the file name.
Dim fName As String = f.Substring(sourceDir.Length + 1)
' Use the Path.Combine method to safely append the file name to the path.
' Will overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)
Next
' Copy text files.
For Each f As String In txtList
'Remove path from the file name.
Dim fName As String = f.Substring(sourceDir.Length + 1)
Try
' Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))
' Catch exception if the file was already copied.
Catch copyError As IOException
Console.WriteLine(copyError.Message)
End Try
Next
For Each f As String In txtList
File.Delete(f)
Next
For Each f As String In picList
File.Delete(f)
Next
Catch dirNotFound As DirectoryNotFoundException
Console.WriteLine(dirNotFound.Message)
End Try
備註
使用參數的任何相對或絕對路徑資訊 path
來指定檔名。 無法包含通配符。 相對路徑資訊會解譯為相對於目前的工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory。
如果要刪除的檔案不存在,則不會擲回例外狀況。
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。