How to create a file from System.IO.Stream in metro apps?

喜你入骨 提交于 2019-12-13 02:23:02

问题


Stream stream = await new HttpClient().GetStreamAsync( url );

How can i create a file in app's local folder having data downloaded through this stream as file content?


回答1:


You could probably do some kind of stream to stream copy. However, I happen to just use the response object in my code:

var client = new HttpClient();
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
    Stream stream = null;
    StorageFolder localFolder = ApplicationData.Current.TemporaryFolder;
    StorageFile file = await localFolder.CreateFileAsync("savename.htm",
        CreationCollisionOption.ReplaceExisting);
    stream = await file.OpenStreamForWriteAsync();

    await response.Content.CopyToAsync(stream);



回答2:


You need to use a StorageFile

Something like this:

StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("yourfile.xml", CreationCollisionOption.ReplaceExisting);

More information at http://social.msdn.microsoft.com



来源:https://stackoverflow.com/questions/15599654/how-to-create-a-file-from-system-io-stream-in-metro-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!