1

Using Visual Studio 2010 and im getting "File is used by another process" almost randomly when trying to read a file. Im reading about 10 xml files into memory with the same procedure

The code that breaks is

private static TextReader CreateTextReader(IsolatedStorageFile isolatedStorageFolder, string path)
        {
            TextReader textReader = null;

            if (isolatedStorageFolder == null)
                textReader = new StreamReader(path);
            else
                textReader = new StreamReader(new IsolatedStorageFileStream(path, FileMode.Open, isolatedStorageFolder));

            return textReader;
        }

The code breaks 10 percent of the time on

 textReader = new StreamReader(path);

I personally think its some kind of garbage collection problem, anyone has any tips on how to debug this kind of problem.

4

2 回答 2

1

请务必在所有可能锁定文件的 Steam 阅读器操作上调用 .Dispose 或 .Close。这可能是您的问题,因为该代码对我来说是一个平面程序。

于 2012-05-21T14:49:23.377 回答
1

您需要处理TextReader. 使用如下using语句

using (TextReader r = CreateTextReader(...))
{
}

否则,当您关闭应用程序时,文件将保持打开状态。

编辑
您在对您实际上已经在使用的问题的评论中说using-您尝试读取的文件是否实际上是由另一个应用程序打开的?有时防病毒解决方案会在扫描文件或类似的东西时锁定文件 - 它会在片刻后工作还是你必须重新启动或类似的东西?

于 2012-05-21T14:49:56.063 回答