k.troy2012
k.troy2012

Reputation: 344

C# UWP Download into AppData: Access Denied

I'm writing code for an app which runs on a Raspberry Pi with Windows IoT. I am developing via Remote on Visual Studio 2017.

I have to download a .zip file via REST and save it into the AppData-Folder. But I am getting this Exception:

Exception thrown: 'System.UnauthorizedAccessException' in System.IO.FileSystem.dll
Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.ni.dll
Access to the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\f6e0d540-943b-4fd7-9d4e-1572ca85cec2VS.Debug_ARM.knhelfen\htmls5000\myZipDownload.zip' is denied.

The download works fine if I change the downloadpath into another folder than AppData.

This is my downloadmethod:

            Uri uri = new Uri(this.url);
            HttpWebRequest getRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            getRequest.Method = this.method;
            getRequest.Headers["Authorization"] = "Bearer " + (this.bearerToken);
            response3 = await getRequest.GetResponseAsync() as HttpWebResponse;
            Stream fileStream = response3.GetResponseStream();

            //Download .zip file to downloadPath
            await Task.Run(() =>
            {
                Task.Yield();
                using (var path = File.Create(downloadPath + "/myZipDownload.zip"))
                {
                    fileStream.CopyTo(path);
                }
            });

Upvotes: 0

Views: 693

Answers (1)

Rita Han
Rita Han

Reputation: 9700

You can Set folder permissions for UWP apps.

And try to use CreateFile2 API to the folder.

Here is a similar question you can reference.

Upvotes: 1

Related Questions