繁体   English   中英

该进程无法访问该文件,因为它正被另一个进程使用 C#

[英]The process cannot access the file because it is being used by another process C#

我在 Temp 中有一个文件,我想删除它,但我遇到了这个异常。

该进程无法访问文件“C:\Users\User1\AppData\Local\Temp\File.txt”,因为它正被另一个进程使用

我尝试了在 StackOverflow 中找到的解决方案,但仍然显示异常。

我怎样才能解决这个问题?

 public void DeleteFile(string ext, string FullName,string path)
        {
            FilePath = System.IO.Path.Combine(path, FullName+ mesh_ext);

            if (File.Exists(FilePath))
            {
                using (var stream = File.Open(FilePath , FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {           
                    File.Delete(FilePath);
                }
            }
        }

您打开一个文件,然后尝试删除它。 只是不要打开文件。

var file = new FileInfo(System.IO.Path.Combine(path, FullName+ mesh_ext));
if(file.Exists)
    file.Delete();

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM