在WP中处理文件 - oplock坏了

时间:2015-02-21 00:07:52

标签: c# windows-phone-8.1

在某些情况下,我试图在Windows Phone 8.1中读取文件并在其他情况下写入文件。我使用以下代码来阅读它:

var folder = ApplicationData.Current.LocalFolder;
try
{
    var connectionsFile = await folder.OpenStreamForReadAsync("connections");
    using (var streamReader = new StreamReader(connectionsFile, Encoding.Unicode))
    {
        while (!streamReader.EndOfStream)
        {
            String con = await streamReader.ReadLineAsync();
            String[] props = con.Split('\t');
            Connection newConnection = new Connection() { Name = props[0], Url = props[1] };
            ConnectionsCollection.Add(newCollection);
        }
        await connectionsFile.FlushAsync();
        connectionsFile.Dispose();
     }
}
catch(Exception e)
{
    //handle exception
}

我的问题是,它通过&#34的内部异常不断地击中了捕获;与此oplock关联的句柄已被关闭。 oplock现在已经坏了。" (我在尝试写入时遇到同样的错误。)我无法弄清问题是什么,特别是因为我成功使用相同的代码在其他两个地方读取相同的文件。

1 个答案:

答案 0 :(得分:0)

我认为您需要删除await connectionsFile.FlushAsync();行,因为您正在使用该文件进行阅读。同时删除connectionsFile.Dispose();并使用connectionsFile赋值中的using(...)

var folder = ApplicationData.Current.LocalFolder;
try
{
    using (var connectionsFile = await folder.OpenStreamForReadAsync("connections"))
    using (var streamReader = new StreamReader(connectionsFile, Encoding.Unicode))
    {
        while (!streamReader.EndOfStream)
        {
            String con = await streamReader.ReadLineAsync();
            String[] props = con.Split('\t');
            Connection newConnection = new Connection() {Name = props[0], Url = props[1]};
            ConnectionsCollection.Add(newCollection);
        }
    }
}
catch (Exception e)
{
    //handle exception
}

我希望它有所帮助。