Este tema aún no ha recibido ninguna valoración - Valorar este tema

FileInfo.Delete (Método)

Elimina de forma permanente un archivo.

Espacio de nombres: System.IO
Ensamblado: mscorlib (en mscorlib.dll)

public override void Delete ()
public void Delete ()
public override function Delete ()
Tipo de excepción Condición

IOException

El archivo de destino se abre o se asigna a memoria en un equipo con Microsoft Windows NT.

SecurityException

El llamador no dispone del permiso requerido.

UnauthorizedAccessException

La ruta de acceso es un directorio.

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

Cómo: Escribir texto en un archivo

Escribir en un archivo de texto.

Cómo: Escribir texto en un archivo

Leer de un archivo de texto.

Cómo: Leer texto de un archivo

Anexar texto a un archivo.

Cómo: Abrir y anexar a un archivo de registro

File.AppendText

FileInfo.AppendText

Copiar un archivo

File.Copy

FileInfo.CopyTo

Cambiar de nombre o mover un archivo

File.Move

FileInfo.MoveTo

Eliminar un directorio

Directory.Delete

DirectoryInfo.Delete

Leer de un archivo binario

Cómo: Leer y escribir en un archivo de datos recién creado

Escribir en un archivo binario.

Cómo: Leer y escribir en un archivo de datos recién creado

Crear un directorio

CreateDirectory

Directory

Ver los archivos de un directorio

Name

Definir los atributos de un archivo

SetAttributes

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.

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0
¿Le ha resultado útil?
(Caracteres restantes: 1500)
Contenido de la comunidad Agregar