我正在开发一个应用程序,需要删除一个文件,无论它是否被其他进程使用。
考虑以下代码片段。
考虑以下代码片段。
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
{
lclFileName=@"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();
}
}
}
我正在删除被同一进程使用的文件。是否可能删除该文件?
谢谢, Amit Shah