使用StreamWriter后文件为空

use*_*060 2 c# streamwriter isolatedstoragefile windows-phone-7

我正在尝试将一些数据写入我项目中的现有文件(项目的本地文件).我使用了以下代码

        Uri path = new Uri(@"Notes.txt", UriKind.Relative);

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {

            using (StreamWriter writefile = new StreamWriter(new IsolatedStorageFileStream(path.ToString(), FileMode.Append, FileAccess.Write,myIsolatedStorage)))
            {
                writefile.WriteLine("hi");
                writefile.Flush();
                writefile.Dispose();
            }
        }
Run Code Online (Sandbox Code Playgroud)

执行程序时没有异常/错误.但是文件为空并且不包含任何数据.

我已将文件的构建操作设置为"资源",将内容设置为"如果更新则复制".只是为了检查,我删除了文件并进行了测试,它仍然没有给出任何异常,尽管我试图在追加模式下打开.

编辑:我在我的开发环境中打开文件进行检查.但是我后来使用ISETool.exe来检查文件.但该文件根本没有创建!! 这是我使用的更新代码:

 Uri path = new Uri(@"Notes.txt", UriKind.Relative);
 using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
 using (var stream = new IsolatedStorageFileStream(path.ToString(), FileMode.OpenOrCreate, FileAccess.Write, myIsolatedStorage))
 using (var writefile = new StreamWriter(stream))
        {
            writefile.WriteLine("hi");
        }
Run Code Online (Sandbox Code Playgroud)

Ric*_*lay 6

编辑

根据您的上述评论,我认为您的问题实际上是您误解了隔离存储的工作原理; 它将文件存储在手机上模拟器图像内,这两者都不是您的开发机器的本机文件系统.

如果您需要从开发机器访问该文件,如果您不介意命令行界面,则需要一个实用程序,如Windows Phone Power Tools(如上面的Alastair Pitts)或SDK的isetool.exe.

原帖

跳出来的两件事:

  1. 你没有处置你的 IsolatedStorageFileStream
  2. 阁下应的实例IsolatedStorageFileStreamIsolatedStorageFile
  3. 你并不需要调用Disposewritefile(这是using做)

试试这个:

using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = isolatedStorage.OpenFile(path.ToString(), FileMode.Append, FileAccess.Write))
using (var writefile = new StreamWriter(stream))
{
    writefile.WriteLine("hi");
}
Run Code Online (Sandbox Code Playgroud)