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