在某些情况下,我尝试在 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
}
我的问题是,它始终如一地命中了一个内部异常“与此 oplock 相关联的句柄已关闭。oplock 现在已损坏。” (我在尝试写入时遇到同样的错误。)我无法弄清楚问题出在哪里,特别是因为我成功地使用相同的代码在其他两个地方读取相同的文件。
最佳答案
我认为您需要删除 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
}
希望对你有帮助。
关于c# - 在 WP 中处理文件——oplock 损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28640354/