Search code examples
c#file-iowindows-8winrt-xaml

Write Stream to file in Windoes 8 class library


I am receiving a stream from a web api. I want to write this stream to a Storage file.


Solution

  • You need to pass the FileStram and FileName

     public async Task SaveToLocalFolderAsync(Stream file, string fileName)
        {
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            StorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            using (Stream outputStream = await storageFile.OpenStreamForWriteAsync())
            {
                await file.CopyToAsync(outputStream);
            }
        }
    

    Saving a stream