Partager via


FileInfo.Delete Méthode

Définition

Supprime définitivement un fichier.

public:
 override void Delete();
public override void Delete ();
override this.Delete : unit -> unit
Public Overrides Sub Delete ()

Exceptions

Le fichier cible est ouvert ou mappé en mémoire sur un ordinateur exécutant Microsoft Windows NT.

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

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

Le chemin est un répertoire.

Exemples

L’exemple suivant illustre la Delete méthode.

using namespace System;
using namespace System::IO;

int main()
{
   String^ path = "c:\\MyTest.txt";
   FileInfo^ fi1 = gcnew FileInfo( path );
   try
   {
      StreamWriter^ sw = fi1->CreateText();
      if ( sw )
         delete (IDisposable^)sw;

      String^ path2 = String::Concat( path, "temp" );
      FileInfo^ fi2 = gcnew FileInfo( path2 );
      
      //Ensure that the target does not exist.
      fi2->Delete();
      
      //Copy the file.
      fi1->CopyTo( path2 );
      Console::WriteLine( "{0} was copied to {1}.", path, path2 );
      
      //Delete the newly created file.
      fi2->Delete();
      Console::WriteLine( "{0} was successfully deleted.", path2 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//c:\MyTest.txt was copied to c:\MyTest.txttemp.
//c:\MyTest.txttemp was successfully deleted.
using System;
using System.IO;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\MyTest.txt";
        FileInfo fi1 = new FileInfo(path);

        try
        {
            using (StreamWriter sw = fi1.CreateText()) {}
            string path2 = path + "temp";
            FileInfo fi2 = new FileInfo(path2);

            //Ensure that the target does not exist.
            fi2.Delete();

            //Copy the file.
            fi1.CopyTo(path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            //Delete the newly created file.
            fi2.Delete();
            Console.WriteLine("{0} was successfully deleted.", path2);
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//c:\MyTest.txt was copied to c:\MyTest.txttemp.
//c:\MyTest.txttemp was successfully deleted.
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\MyTest.txt"
        Dim fi As FileInfo = New FileInfo(path)

        Try
            Dim sw As StreamWriter = fi.CreateText()
            sw.Close()
            Dim path2 As String = path + "temp"
            Dim fi2 As FileInfo = New FileInfo(path2)

            'Ensure that the target does not exist.
            fi2.Delete()

            'Copy the file.
            fi.CopyTo(path2)
            Console.WriteLine("{0} was copied to {1}.", path, path2)

            'Delete the newly created file.
            fi2.Delete()
            Console.WriteLine("{0} was successfully deleted.", path2)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'c:\MyTest.txt was copied to c:\MyTest.txttemp.
'c:\MyTest.txttemp was successfully deleted.

L’exemple suivant crée, ferme et supprime un fichier.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Create a reference to a file.
   FileInfo^ fi = gcnew FileInfo( "temp.txt" );
   
   // Actually create the file.
   FileStream^ fs = fi->Create();
   
   // Modify the file as required, and then close the file.
   fs->Close();
   
   // Delete the file.
   fi->Delete();
}
using System;
using System.IO;

public class DeleteTest
{
    public static void Main()
    {
        // Create a reference to a file.
        FileInfo fi = new FileInfo("temp.txt");
        // Actually create the file.
        FileStream fs = fi.Create();
        // Modify the file as required, and then close the file.
        fs.Close();
        // Delete the file.
        fi.Delete();
    }
}
Imports System.IO

Public Class DeleteTest
    Public Shared Sub Main()
        ' Create a reference to a file.
        Dim fi As New FileInfo("temp.txt")
        ' Actually create the file.
        Dim fs As FileStream = fi.Create()
        ' Modify the file as required, and then close the file.
        fs.Close()
        ' Delete the file.
        fi.Delete()
    End Sub
End Class

Remarques

Si le fichier n’existe pas, cette méthode ne fait rien.

S’applique à

Voir aussi