File.Delete(String) Méthode

Définition

Supprime le fichier spécifié.

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)

Paramètres

path
String

Nom du fichier à supprimer. Les caractères génériques ne sont pas pris en charge.

Exceptions

.NET Framework et versions de .net Core antérieures à 2,1 : path est une chaîne de longueur nulle, ne contient que des espaces blancs ou contient un ou plusieurs caractères non valides. Vous pouvez rechercher les caractères non valides à l’aide de la méthode GetInvalidPathChars().

path a la valeur null.

Le chemin spécifié n’est pas valide (par exemple, il est sur un lecteur non mappé).

Le fichier spécifié est en cours d’utilisation.

  • ou - Un handle est ouvert sur le fichier, et le système d’exploitation est Windows XP ou une version antérieure. Ce handle ouvert peut être le résultat d’une énumération de répertoires et de fichiers. Pour plus d’informations, consultez Comment : énumérer des répertoires et fichiers.

path est dans un format non valide.

Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.

L'appelant n'a pas l'autorisation requise.

  • ou - Le fichier est un fichier exécutable en cours d’utilisation.

  • ou - path est un répertoire.

  • ou - path a spécifié un fichier en lecture seule.

Exemples

L’exemple suivant copie des groupes de fichiers dans le dossier de sauvegarde C:\archives\2008, puis les supprime du dossier source.

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

Remarques

Spécifiez un nom de fichier avec des informations de chemin d’accès relatif ou absolu pour le path paramètre. Les caractères génériques ne peuvent pas être inclus. Les informations relatives au chemin d’accès relatif sont interprétées par rapport au répertoire de travail actuel. Pour obtenir le répertoire de travail actuel, consultez GetCurrentDirectory .

Si le fichier à supprimer n’existe pas, aucune exception n’est levée.

Pour obtenir la liste des tâches d’e/s courantes, consultez tâches d’e/s courantes.

S’applique à

Voir aussi