简体   繁体   中英

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);
                }
            });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM