File.Delete(String) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Elimina il file specificato.
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)
Parametri
- path
- String
Nome del file da eliminare. I caratteri jolly non sono supportati.
Eccezioni
.NET Framework versioni di .NET Core precedenti alla 2.1: è una stringa di lunghezza zero, contiene solo spazi vuoti o contiene uno o più path
caratteri non validi. È possibile cercare i caratteri non validi usando il metodo GetInvalidPathChars().
path
è null
.
Il percorso specificato non è valido, ad esempio si trova in un'unità non mappata.
Il file specificato è in uso.
-oppure- È presente un handle aperto sul file e il sistema operativo è Windows XP o versioni precedenti. Questo handle aperto può derivare dall'enumerazione di directory e file. Per altre informazioni, vedere Procedura: enumerare directory e file.
Il formato di path
non è valido.
Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.
Il chiamante non dispone dell'autorizzazione richiesta.
-oppure- Il file è un file eseguibile in uso.
-oppure-
path
è una directory.
-oppure-
path
ha specificato un file di sola lettura.
Esempio
L'esempio seguente copia gruppi di file nella cartella di backup C:\archives\2008 e li elimina dalla cartella di origine.
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
Commenti
Specificare un nome file con le informazioni sul percorso relativo o assoluto per il path
parametro . I caratteri jolly non possono essere inclusi. Le informazioni sul percorso relativo vengono interpretate come relative alla directory di lavoro corrente. Per ottenere la directory di lavoro corrente, vedere GetCurrentDirectory .
Se il file da eliminare non esiste, non viene generata alcuna eccezione.
Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.