Search code examples
c#restdownloaduwpwindowsiot

C# write stream into a .zip-file on Windows IoT


I've written an UWP-App to download files via REST-Requests. The App runs on a Raspberry Pi 3 with Windows IoT.

I want to write 'testStream' into a .zip-File and download it. . Here is my code:

try
        {
            Uri uri = new Uri("http://xxxx:3080/cds/api/download/book/46795403-de-DE");
            HttpWebRequest getRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            getRequest.Method = "GET";
            getRequest.Headers["Authorization"] = "Bearer xyxyxy";
            HttpWebResponse response2 = await getRequest.GetResponseAsync() as HttpWebResponse;
            StreamReader streamReader2 = new StreamReader(response2.GetResponseStream());
            Stream testStream = response2.GetResponseStream();
            getResult = streamReader2.ReadToEnd();
            textBox2.Text = getResult;

            await Task.Run(() =>
            {
                Task.Yield();
                using (var fs = File.Create("\\myZipDownload.zip"))
                {
                    testStream.CopyTo(fs);
                }
            });
        }

I'm getting this error:

"Access to the path 'C:\myZipDownload.zip' is denied."

Solution

  • You need a writable destination.

    You can use Environment.GetFolderPath to get a writable path.

    how-to-get-temporary-folder-for-current-user