【问题标题】:How Write a Stream into a xml file in Isolated Storage如何将流写入隔离存储中的 xml 文件
【发布时间】:2012-05-20 00:56:07
【问题描述】:

我正在尝试在我的应用中实施 SkyDrive 备份。一切正常,除了我不知道如何轻松地将流(从 SkyDrive 下载的备份)保存到独立存储中。

我知道我可以对流进行反序列化而不是保存它,但这会不必要地复杂。

所以,这是我的问题:

我有一个包含 SkyDrive 文件的流,我需要将它作为文件“Settings.XML”保存到独立存储中。

所以基本上我需要将“流”写入独立存储中的 Settings.xml 文件

        static void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e, string saveToPath)
    {
        Stream stream = e.Result; //Need to write this into IS

        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(saveToPath, FileMode.Create))
                {
                    //How to save it?

                }
            }
        }...

谢谢!

【问题讨论】:

    标签: c# windows-phone-7 stream isolatedstorage onedrive


    【解决方案1】:

    使用 steam 的 CopyTo() 方法 - http://msdn.microsoft.com/en-us/library/dd782932.aspx

    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(saveToPath, FileMode.Create))    
    {    
      stream.CopyTo(fileStream);
    } 
    

    【讨论】:

      【解决方案2】:

      最快和最简单的做法是将字节逐块从一个流复制到下一个流 - 请参阅How do I copy the contents of one stream to another?中接受的答案


      但是,如果网络流有可能损坏或包含无效数据,则可能值得在保存之前反序列化流并进行验证。

      【讨论】:

      • 好的,我将SkyDrive流的内容复制到本地IsolatedStorage Stream中。但是我将如何保存它(到文件中)?
      • 您的代码示例中的 iso 流由一个文件支持 :) 这就是 openFile 为您提供的。
      • using 表示将在流上调用Dispose() - 这意味着Close() 发生
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      相关资源
      最近更新 更多