Skip to main content
 首页 » 编程设计

c#之如何将流写入独立存储中的 xml 文件

2024年02月20日13lvdongjie

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

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

所以,这是我的问题:

我有一个包含来自 SkyDrive 的文件的流,我需要将其作为文件“Settings.XML”保存到 IsolatedStorage 中。

所以基本上我需要将“流”写入独立存储中的 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? 
 
                } 
            } 
        }... 

谢谢!

请您参考如下方法:

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

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