File.Delete(String) File.Delete(String) File.Delete(String) File.Delete(String) Method

定義

刪除指定的檔案。Deletes the specified file.

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 String String String

要刪除的檔案的名稱。The name of the file to be deleted. 不支援萬用字元。Wildcard characters are not supported.

例外狀況

path 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

指定的檔案正在使用中。The specified file is in use.

-或--or- 檔案上有開啟的控制代碼,而且作業系統是 Windows XP 或之前的版本。There is an open handle on the file, and the operating system is Windows XP or earlier. 這個開啟的控制代碼可能是列舉目錄和檔案所產生的結果。This open handle can result from enumerating directories and files. 如需詳細資訊,請參閱如何:列舉目錄與檔案For more information, see How to: Enumerate Directories and Files.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

呼叫端沒有必要的使用權限。The caller does not have the required permission.

-或--or- 檔案是使用中的可執行檔。The file is an executable file that is in use.

-或--or- path 是目錄。path is a directory.

-或--or- path 指定唯讀檔案path specified a read-only file.

範例

下列範例會複製 C:\archives\2008 備份資料夾中的檔案群組,並會加以刪除來源資料夾。The following example copies groups of files to the C:\archives\2008 backup folder and then deletes them from the source folder.

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);
}

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參數。Specify a file name with any relative or absolute path information for the path parameter. 萬用字元不能包含項目。Wildcard characters cannot be included. 相對於目前工作目錄,會解譯相對路徑的資訊。Relative path information is interpreted as relative to the current working directory. 若要取得目前工作目錄,請參閱GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

如果要刪除的檔案不存在,則會擲不回任何例外狀況。If the file to be deleted does not exist, no exception is thrown.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
刪除指定的檔案。for deleting the specified file. 相關聯的列舉型別: WriteAssociated enumeration: Write

適用於

另請參閱