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 les versions .NET Core antérieures à 2.1 : path est une chaîne de longueur nulle, contient uniquement un espace blanc ou contient un ou plusieurs caractères non valides. Vous pouvez rechercher des 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 se trouve sur un lecteur non mappé).

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

-ou-

Il existe un handle ouvert sur le fichier, et le système d’exploitation est Windows XP ou antérieur. Ce handle ouvert peut résulter de l’énumération des répertoires et des fichiers. Pour plus d’informations, consultez Guide pratique pour énumérer des répertoires et des fichiers.

path est dans un format non valide.

Le chemin d’accès spécifié, le nom de fichier ou les deux 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 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);
}
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

Remarques

Spécifiez un nom de fichier avec toutes les informations relatives ou absolues du chemin d’accès pour le path paramètre. Les caractères génériques ne peuvent pas être inclus. Les informations relatives au chemin d’accès sont interprétées comme relatives 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