c#检测一个文件是否可以被删除
c# Detect if a file is able to be deleted
我正在尝试完成检测文件是否可以删除的简单任务,例如:如果另一个程序正在使用 .dll
,则您无法删除 .dll
- 这就是我希望能够检测到的。是的,希望这是有道理的。
到目前为止,我已经尝试了以下但没有成功:
try
{
System.IO.File.Delete(@"C:\Program Files\MyTestFiles\testing.dll");
}
catch
{
Console.WriteLine("Unable to delete file");
}
我试过了:
System.IO.File.Delete(@"C:\Program Files\MyTestFiles\testing.dll");
if(File.Exists(@"C:\Program Files\MyTestFiles\testing.dll"))
{
Console.WriteLine("Error: Unable to delete file");
}
else
{
Console.WriteLine("Successfully deleted file!");
}
private bool IsFileLocked()
{
try
{
using (File.Open(@"C:\Program Files\MyTestFiles\testing.dll", FileMode.Open))
{
return false;
}
}
catch (IOException e)
{
// file locked
return true;
}
}
或者为了获得更好的解决方案,您可以使用:How do I find out which process has a file open?
我正在尝试完成检测文件是否可以删除的简单任务,例如:如果另一个程序正在使用 .dll
,则您无法删除 .dll
- 这就是我希望能够检测到的。是的,希望这是有道理的。
到目前为止,我已经尝试了以下但没有成功:
try
{
System.IO.File.Delete(@"C:\Program Files\MyTestFiles\testing.dll");
}
catch
{
Console.WriteLine("Unable to delete file");
}
我试过了:
System.IO.File.Delete(@"C:\Program Files\MyTestFiles\testing.dll");
if(File.Exists(@"C:\Program Files\MyTestFiles\testing.dll"))
{
Console.WriteLine("Error: Unable to delete file");
}
else
{
Console.WriteLine("Successfully deleted file!");
}
private bool IsFileLocked()
{
try
{
using (File.Open(@"C:\Program Files\MyTestFiles\testing.dll", FileMode.Open))
{
return false;
}
}
catch (IOException e)
{
// file locked
return true;
}
}
或者为了获得更好的解决方案,您可以使用:How do I find out which process has a file open?