首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >追加到IsolatedStorageFile时出错

追加到IsolatedStorageFile时出错
EN

Stack Overflow用户
提问于 2012-01-11 08:33:13
回答 2查看 335关注 0票数 0

我在独立文件存储方面遇到了一些问题,我正在尝试将其追加到一个文件中,但是当我使用下面的代码时,我得到了关于此行上无效参数的错误

代码语言:javascript
复制
IsolatedStorageFileStream("Folder\\barcodeinfo.txt", FileMode.Append, 
                                     FileMode.OpenOrCreate, myStore))

我想这和Filemode.Append有关..我正在尝试附加到文件中,而不是创建新的

代码语言:javascript
复制
// Obtain the virtual store for the application.
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
// Create a new folder and call it "MyFolder".
myStore.CreateDirectory("Folder");

// Specify the file path and options.
using (var isoFileStream = new IsolatedStorageFileStream("Folder\\barcodeinfo.txt", FileMode.Append, FileMode.OpenOrCreate, myStore))
{
      //Write the data
      using (var isoFileWriter = new StreamWriter(isoFileStream))
      {
            isoFileWriter.WriteLine(textBox1.Text);
            isoFileWriter.WriteLine(textBox2.Text);
            isoFileWriter.WriteLine(textBox3.Text);
      }
}
EN

回答 2

Stack Overflow用户

发布于 2012-01-11 08:44:04

不存在需要两个FileModes的重载。它应该是

代码语言:javascript
复制
IsolatedStorageFileStream("Folder\\barcodeinfo.txt", FileMode.Append, 
                                     FileAccess.Write, myStore));

关于FileMode.Append需要注意的重要一点是:

如果文件存在,

FileMode.Append将打开该文件并查找到文件的末尾,或者创建一个新文件。Append只能与Write一起使用。尝试查找到文件末尾之前的位置将抛出IOException,任何读取尝试都会失败并抛出NotSupportedException。

这就是使用FileAccess.Write的原因。

票数 2
EN

Stack Overflow用户

发布于 2012-01-11 08:42:59

看起来你有FileMode.Append, FileMode.OpenOrCreate。这是两种文件模式。第一个参数是FileMode,第二个参数应该是FileAccess

这应该会解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8812608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档