简体   繁体   English

File.Delete错误“该进程无法访问该文件,因为该文件正在被另一个进程使用”

[英]File.Delete error “The process cannot access the file because it is being used by another process”

I have written a DotUnit test suite for testing some data import functionality in my application. 我已经编写了一个DotUnit测试套件,用于测试应用程序中的某些数据导入功能。 It works by making a backup of some local Microsoft Access Database, let's call it 'Test.mdb', to 'Test.mdb.bak', performing some data import (and subsequent Assert checks) and then restoring the original from the backup. 它通过对某些本地Microsoft Access数据库进行备份来工作,我们将其称为“ Test.mdb”,再备份为“ Test.mdb.bak”,执行一些数据导入(以及随后的Assert检查),然后从备份中还原原始数据。

The SetUp() function creates a backup if one doesn't exist. 如果不存在,SetUp()函数将创建一个备份。

The TearDown() function attempts to delete 'Test.mdb' and then copy 'Test.mdb.bak' to 'Test.mdb'. TearDown()函数尝试删除“ Test.mdb”,然后将“ Test.mdb.bak”复制到“ Test.mdb”。

Intermittently running the tests fail with this error "The process cannot access the file because it is being used by another process". 间歇运行测试失败,并显示以下错误“该进程无法访问该文件,因为该文件正在被另一个进程使用”。

I've had a look the MSDN on File.Delete and IO permissions but couldn't find what I was after. 我看了一下File.Delete和IO权限上的MSDN,但找不到我想要的。 Does anyone know if there is a .NET feature that will allow me to completely lock the file before attempting to delete it? 有谁知道是否有.NET功能可以让我在尝试删除文件之前将其完全锁定? Or find which process is accessing it at the time of deletion? 还是在删除时查找哪个进程正在访问它?

You might reconsider your testing approach. 您可能会重新考虑您的测试方法。 Instead: 代替:

  1. Create a temporary copy of the file 创建文件的临时副本
  2. Perform the actions that are being tested on the temp file 执行临时文件上正在测试的操作
  3. Release all handles (close all connections) to the temp file 释放所有句柄(关闭所有连接)到临时文件
  4. Delete the temp file 删除临时文件

Using this pattern, the only process that will be accessing the file will be the thread running the unit test. 使用此模式,唯一访问文件的进程将是运行单元测试的线程。

Use the function: System.IO.Path.GetTempFileName(); 使用以下函数: System.IO.Path.GetTempFileName();

http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename.aspx http://msdn.microsoft.com/zh-CN/library/system.io.path.gettempfilename.aspx

EDIT: Here is one way to code it: 编辑:这是一种编码方式:

var tempFile = System.IO.Path.GetTempFileName();
System.IO.File.Copy(@"C:\Test.mdb", tempFile, true);
// 2. Test tempFile
// 3. Release handles to tempFile, use a using statement around any 
//    streams or System.IO API's that are using the file in any way.
System.IO.File.Delete(tempFile);

I had a similar issue while unit testing Entity Framework code using a SQLite database where each test needed to use a fresh instance of a database, so my the [TestCleanup] method was doing a File.Delete on the database, but was getting the same "used by another process" error. 在使用SQLite数据库对实体框架代码进行单元测试时,我遇到了类似的问题,其中每个测试都需要使用数据库的新实例,因此我的[TestCleanup]方法正在数据库上执行File.Delete,但是却得到了相同的结果“由另一个进程使用”错误。

Before I called, File.Delete, I had to add the following to fix my issue. 在调用File.Delete之前,我必须添加以下内容来解决我的问题。

GC.Collect(); GC.Collect();

GC.WaitForPendingFinalizers(); GC.WaitForPendingFinalizers();

[TestInitialize]
public void MyTestInitialize()
{
    // Copies the embedded resource 'MyDatabase.db' to the Testing Directory
    CommonTestFixture.UnpackFile("MyDatabase.db", this.GetType(), this.TestContext.TestDeploymentDir);
}

[TestCleanup]
public void MyTestCleanup()
{
    // Adding the following two lines of code fixed the issue
    GC.Collect();
    GC.WaitForPendingFinalizers();

    // Removes 'MyDatabase.db' from the testing directory.
    File.Delete(Path.Combine(this.TestContext.TestDeploymentDir, "MyDatabase.db"));
}

[TestMethod]
public void GetVenueTest()
{
    // CreateTestEntities() is a helper that initializes my entity framework DbContext
    // with the correct connection string for the testing database.
    using (var entityFrameworkContext = CreateTestEntities())
    {
        // Do whatever testing you want here:
        bool result = entityFrameworkContext.TestSomething()
        Assert.IsTrue(result);
    }
}

These two lines solve the issue. 这两行解决了这个问题。

GC.Collect();
GC.WaitForPendingFinalizers();

I have tested this in my code and it worked well. 我已经在我的代码中对此进行了测试,并且效果很好。

--- Jeetendra -吉顿德拉

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 File.Delete进程无法访问文件,因为它正在被另一个进程使用 - File.Delete the process cannot access the file because it is being used by another process File.Delete无法删除文件,因为它正在被另一个进程使用 - File.Delete cannot delete a file because it is being used by another process File.Delete无法删除文件,因为它正在被另一个进程使用。 不确定如何解决此问题 - File.Delete cannot delete a file because it is being used by another process. Not sure how to fix this File.Delete被另一个进程使用 - File.Delete being used by another process 错误:进程无法访问文件“...”,因为它正由另一个进程使用 - Error: The process cannot access the file '…' because it is being used by another process 错误:“该进程无法访问该文件,因为它正被另一个进程使用” - ERROR: "The process cannot access the file because it is being used by another process" 错误:该进程无法访问文件“ ...”,因为它正在被另一个进程使用 - Error: The process cannot access the file '…' because it is being used by another process 删除文件,错误无法访问该文件,因为该文件正在被另一个进程使用 - delete file and error cannot access the file because it is being used by another process C# File.Delete,另一个进程正在使用的文件 - C# File.Delete, file being used by another process System.IO.File.Delete 抛出“该进程无法访问该文件,因为它正被另一个进程使用” - System.IO.File.Delete throws "The process cannot access the file because it is being used by another process"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM