我创建了一个简单的程序来删除C#中的临时文件(为了好玩,而不是一个主要项目),并且遇到锁定文件(使用中)问题.你通常如何排除这些文件?作为参考,我收到错误:
该进程无法访问文件'ExchangePerflog_8484fa31c65c7a31cfcccd43.dat',因为它正由另一个进程使用.
码:
static void Main(string[] args)
{
string folderPath = string.Empty;
folderPath = System.Environment.GetEnvironmentVariable("temp");
deleteFilesInDirectory(folderPath);
}
public static void deleteFilesInDirectory(string folderPath)
{
try
{
var dir = new DirectoryInfo(folderPath);
dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
dir.Delete(true);
MessageBox.Show(folderPath + " has been cleaned.");
}
catch (System.IO.IOException ex)
{
MessageBox.Show(ex.Message);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
dkn*_*ack 16
无法删除当前正由另一个进程使用的文件.但是你可以等到文件没有被锁定.
检查while循环,直到使用此方法解锁文件
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
Run Code Online (Sandbox Code Playgroud)
FileInfo file = new FileInfo("PathToTheFile");
while (IsFileLocked(file))
Thread.Sleep(1000);
file.Delete();
Run Code Online (Sandbox Code Playgroud)
如果要跳过锁定的文件,可以执行此操作.
//
var dir = new DirectoryInfo(folderPath);
foreach(var file in dir.GetFiles()) {
try
{
file.Delete();
}
catch (IOException)
{
//file is currently locked
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
好吧,我遇到了类似的问题。当您在删除文件后不久尝试删除目录时,您必须强制 GC 从当前线程释放文件句柄
public void DisposeAfterTest(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
GC.Collect();
GC.WaitForPendingFinalizers();
if (Directory.Exists(this.TempTestFolderPath))
{
Directory.Delete(this.TempTestFolderPath, true);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
试试下面的代码。只需在文件删除前添加两行:
GC.Collect();
GC.WaitForPendingFinalizers();
Run Code Online (Sandbox Code Playgroud)
我不相信有任何方法可以提前知道该文件是否正在使用。您可以尝试获取文件的独占锁;但这样你就只是用一种例外来换取另一种例外。
如果您正在打开这些文件,请看看是否可以更好地关闭它们。如果它比这更复杂 - 您可以维护一个“删除列表”并继续重试删除直到成功(可能在具有并发集合的另一个线程上)。
我也不相信有办法强制删除正在使用的文件。
| 归档时间: |
|
| 查看次数: |
53456 次 |
| 最近记录: |