【问题标题】:How to write into an existing textfile in windows phone?如何在 Windows Phone 中写入现有的文本文件?
【发布时间】:2013-09-07 10:00:34
【问题描述】:

如果这是在我的解决方案资源管理器中打开文本文件“word.txt”的代码。

       Stream txtStream = Application.GetResourceStream(new   Uri("/sample;component/word.txt", UriKind.Relative)).Stream;

        using (StreamReader sr = new StreamReader(txtStream))
        {
            string jon;
            while (!sr.EndOfStream) 
            {
              jon = sr.ReadLine();
              mylistbox.ItemSource = jon;
            }
        }

如何在现有的文本文件中写入和追加?

【问题讨论】:

  • 创建一个StreamWriter,文档会告诉你如何设置追加模式。
  • StreamWriter 不够用,不能写入应用程序资源。在修改文件之前,您必须将文件复制到隔离存储中

标签: c# windows-phone-7 filestream file-handling


【解决方案1】:

这是一个例子

    public static void WriteBackgroundSetting(string currentBackground)
    {
        const string fileName = "RecipeHub.txt";
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if(myIsolatedStorage.FileExists(fileName))
                myIsolatedStorage.DeleteFile(fileName);
            var stream = myIsolatedStorage.CreateFile(fileName);
            using (StreamWriter isoStream = new StreamWriter(stream))
            {
                isoStream.WriteLine(currentBackground);
            }
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多