2

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

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

1 回答 1

6

编辑

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

如果您需要从您的开发机器访问该文件,您将需要一个实用程序,如Windows Phone Power Tools(c/o 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");
}
于 2013-04-11T03:33:59.923 回答