的FileStream不关闭文件 [英] FileStream not closing file

查看:133
本文介绍了的FileStream不关闭文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

using (MemoryStream str = new MemoryStream())
      {
             Program.api.GetDocument(result, str);
             using (FileStream fileStream = File.Create(filePath))
             {
                    str.WriteTo(fileStream);
             }
      }



当一个文件被写入,它总是事后锁定 - 在尝试删除或修改使Windows告诉我的文件正在使用中,即使在关闭我的应用程序后。我缺少的东西吗?

Whenever a file is written, it is always locked afterwards - attempting to delete it or modify it causes Windows to tell me the file is in use, even after closing my application. Am I missing something?

推荐答案

您的问题是最有可能由Windows搜索索引是的 Windows搜索。如果您试图立即(或者很快)修改后访问该文件,你可能会碰到的那种,你所看到的问题。解决这个问题的最好办法是重试逻辑添加到正在执行的文件操作,它等待的时间和重新尝试文件操作一些小的周期。

Your problem is most likely caused by Windows Search Indexing which is a part of Windows Search. If you attempt to access the file immediately (or very shortly) after modifying it, you may run into the sort of issues you are seeing. The best way around this is to add retry logic to the file operation you are performing, which waits some small period of times and re-attempts the file op.

如果您想确认问题原因是Windows文件搜索索引,可以的禁用的文件类型和/或位置,你写你的文件,看看是否能使得问题消失。

If you would like to confirm that the problem is cause by Windows File Search Indexing, you can disable it for the file type and/or location where you are writing your file to see if that makes the problem go away.

这篇关于的FileStream不关闭文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆