2

在我的记录器应用程序中,我需要打开文件进行记录。我在应用程序关闭时关闭流。但是,我也需要支持读取日志文件的内容。所以当我尝试打开日志文件时,我遇到了 IsolatedStorageException-“Operation not在 Isolatestoragefilestream 上允许"

这是示例代码:

日志文件创建:

IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
    file.CreateDirectory("/log");                
    var stream = file.OpenFile("/log/sample.log", 
    System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite,
    System.IO.FileShare.ReadWrite);

日志入口代码:

byte[] buffer = System.Text.Encoding.UTF8.GetBytes("Hello World");
    stream.Write(buffer,0,buffer.Length);
    stream.Flush();

我可能需要阅读内容:

IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
//here i encounter isolated storage exception
    var stream = file.OpenFile("/log/sample.log", System.IO.FileMode.Open, 
    System.IO.FileAccess.Read,System.IO.FileShare.Read); <br>
    byte [] buffer = new byte[1024];<br>
    stream.Read(buffer, 0, buffer.Length);

我只是试图以读写模式打开一个示例文件,然后再次以读取模式单独打开它,但我仍然得到同样的错误。在 wp7 中,我们不能在文件打开时读取文件吗?(我的意思是......不关闭首次打开时流式传输>)。

请让我知道任何建议。

4

2 回答 2

0

您需要更改读取操作标志。根据MSDN文章:

读取 允许随后打开文件进行读取。

在您的情况下,最好使用 ReadWrite 模式:

ReadWrite允许随后打开文件以进行读取或写入。

于 2013-01-03T10:08:42.413 回答
0

据我说:

在应用程序启动时打开文件读取和写入模式,并使用相同的文件实例来读取和写入文件内容。意味着不要单独打开文件进行读取和写入。

于 2013-01-04T07:14:50.110 回答