C# 使用StreamWriter后,文件为空

C# 使用StreamWriter后,文件为空,c#,windows-phone-7,streamwriter,isolatedstoragefile,C#,Windows Phone 7,Streamwriter,Isolatedstoragefile,我正在尝试将一些数据写入项目中的现有文件(项目的本地文件)。我使用了以下代码 Uri path = new Uri(@"Notes.txt", UriKind.Relative); using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { using (StreamWriter wr

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

        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();
            }
        }
执行程序时没有异常/错误。但是,该文件为空且不包含任何数据

我已将文件的构建操作设置为“资源”,将内容设置为“更新时复制”。 只是为了检查,我删除了该文件并进行了测试,它仍然没有给出任何异常,尽管我正在尝试以追加模式打开

编辑:我正在开发环境中打开文件进行检查。但是,我后来使用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");
        }
编辑

根据您上面的评论,我认为您的问题实际上是您误解了独立存储的工作原理;它将文件存储在手机上或emulator映像中,这两种映像都不是开发机器的本机文件系统

如果您需要从开发机器访问该文件,您将需要一个类似(上面的c/o)的实用程序,或者如果您不介意命令行界面的话

原创帖子

有两件事要向我跳出来:

  • 您没有处理您的
    隔离存储文件流
  • 您应该从
    IsolatedStorageFile
  • 您不需要在
    writefile
    上调用
    Dispose
    (这就是
    使用
    所做的)
  • 试试这个:

    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");
    }
    

    您如何检查文件是否有任何数据?我手动打开文件以从何处手动检查任何数据?您是否使用IsolatedStorageTool从应用程序下载了它?否。我在mainpage.xaml所在的同一文件夹中创建了文件-Notes.txt。我正试图写入该文件。我的观点是:您如何查看该文件以查看它是否已更改?要查看它是否已更改,您必须从模拟器/设备下载它。它不会在您的开发环境中改变。我尝试了上面的代码。相同的结果:(它是否与生成操作或内容有关?或者与文件路径有关?我将该文件放在mainpage.xaml所在的同一文件夹中。请检查我编辑的问题。当我尝试将所有文件下载到本地驱动器时,下载了一个文件夹“IsolatedStore”。该文件夹不包含任何文件“Notes.txt”完全正确。如果无法写入文件系统,它将引发异常。您是否尝试过调试和检查代码是否实际执行?如果尝试过,请稍后尝试打开文件(在单独的使用块中)以查看数据是否可读。我直接提供了文件名,而不是将其作为URI提供。它成功了!谢谢:)