首页 > 解决方案 > Windows 应用程序 (C#) - 进程无法访问该文件,因为它正被另一个进程使用

问题描述

我正在用 C# 编写一个 winform 应用程序来更新图像。

首先,我检查oldfilepathnewfilepath是否相似,如果不相似,则删除旧图像并将新图像添加到创建的文件夹(命名为“图像”),并更新数据库中的新文件路径。

但是当我点击更新按钮时,它会显示错误

"The process cannot access the file because it is being used by another process"

string oldpath;
if (File.Exists(Path.Combine(oldpath))) //oldpath where i'm getting the old file path
{
    File.Delete(Path.Combine(oldpath));
    if (File.Exists(Path.Combine(oldpath)))
    {
        File.Delete(Path.Combine(oldpath));
    }
}

有人可以帮我吗?

标签: imagereplace

解决方案


oldpath 中提到的文件可能在其他进程中实际打开(例如,用它打开的记事本)。检查是否如此,然后关闭该进程。


推荐阅读