FileInfo.Delete (Método)
Ensamblado: mscorlib (en mscorlib.dll)
Si el archivo no existe, este método no realiza ninguna operación.
En la siguiente tabla se muestran ejemplos de otras tareas de E/S típicas o relacionadas.
| Para realizar esta operación... | Vea el ejemplo de este tema... |
|---|---|
| Crear un archivo de texto | |
| Escribir en un archivo de texto. | |
| Leer de un archivo de texto. | |
| Anexar texto a un archivo. | |
| Copiar un archivo | |
| Cambiar de nombre o mover un archivo | |
| Eliminar un directorio | |
| Leer de un archivo binario | |
| Escribir en un archivo binario. | |
| Crear un directorio | |
| Ver los archivos de un directorio | |
| Definir los atributos de un archivo |
Nota de la plataforma Windows NT 4.0: Delete no elimina un archivo que está abierto para la operación normal de E/S ni un archivo asignado a memoria.
En el siguiente ejemplo se muestra el método Delete.
using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\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()); } } }
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
String path = "c:\\temp\\MyTest.txt";
FileInfo fi1 = new FileInfo(path);
try {
StreamWriter sw = fi1.CreateText();
try {
}
finally {
sw.Dispose();
}
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 (System.Exception e) {
Console.WriteLine("The process failed: {0}", e.ToString());
}
} //main
} //Test
En el siguiente ejemplo se crea, se cierra y se elimina un archivo.
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(); } }
import System.*;
import System.IO.*;
public class DeleteTest
{
public static void main(String[] args)
{
// 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();
} //main
} //DeleteTest
import System; import System.IO; public class DeleteTest { public static function Main() : void { // Create a reference to a file. var fi : FileInfo = new FileInfo("temp.txt"); // Actually create the file. var fs : FileStream = fi.Create(); // Modify the file as required, and then close the file. fs.Close(); // Delete the file. fi.Delete(); } } DeleteTest.Main();
- FileIOPermission para leer y escribir en archivos. Enumeración asociada: FileIOPermissionAccess.Write.
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.