Sto sviluppando un'applicazione che richiede di eliminare un file, non importa è in uso da un altro processoCancellare un file con forza, anche se è utilizzato da l'altro processo
Si consideri il seguente frammento di codice.
using System;
using System.IO;
namespace DotNet_Concepts.File_Operation
{
class Deleting_File_Which_Is_In_Use
{
static void Main(string[] args)
{
StreamReader lclFileStream = null;
string lclFileName=string.Empty;
try
{
[email protected]"E:\Visual Studio 2008 Projects\DotNet Concepts\DotNet Concepts\Local Files\Garbage.txt";
if (File.Exists(lclFileName))
{
lclFileStream = new StreamReader(lclFileName);
if (lclFileStream != null)
{
//Doing some operation
}
//Deleting the file before closing the stream
File.Delete(lclFileName);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
Console.ReadLine();
}
}
}
Sto cancellando il file utilizzato dallo stesso processo. E 'possibile eliminare il file
Grazie, Amit Shah
ma il file è in uso. Allora, perché eliminarlo? –
Duplicate - http://stackoverflow.com/questions/1040/how-do-i-delete-a-file-which-is-locked-by-another-process-in-c? –
Non è chiaro dalla tua domanda se vuoi chiudere un file che hai aperto. Se questo è il caso, usa il consiglio di Ed Swangren. –