简体   繁体   中英

File Access Denied Exeption

I wrote a simple c# code to Post an audio file in HttpMultipartForm format, and when I try to access to the file in my hard disk, it throws an exception that access to path is denied.

using(var httpClient = new HttpClient())
{

   using(var request = new HttpRequestMessage(new HttpMethod("POST"),"https://www.iotype.com/api/recognize/file"))
   {

      multipartContent.Add(new StringContent("MyToken"), "token");
      multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\\audio.mp3")), "audio", Path.GetFileName("AUDIO_FILE.mp3"));

      request.Content = multipartContent;

      var response = await httpClient.SendAsync(request);
      txtStatus.Text = response.Content.ToString();

    }
 }

UWP applications are restricted in terms of the directories and files they can access. The easiest way to navigate the restrictions is to use the WinRT types in the Windows.Storage namespace, the two primary classes being StorageFolder and StorageFile, In Windows Runtime for Windows 8 and 8.1, you couldn't use FileStream or the Directory/File classes at all. This made it harder to write portable class libraries, so this restriction has been relaxed in UWP for Windows 10, although the limits on what directories and files you can access still apply. So its not possible to use File Class in UWP app.

In UWP Applications there is a lot of restriction because it's applications are embed into windows, imposed for reasons of architecture but also of security (Isolation of Application Domain ) , it does not prevent that it exists Several ways to manage information (Reading, Writing) using Windows API in a dedicated application location.

Different Storage For UWP Application :

  • The local storage :

    It's a default file system for application and it's identified by LocalFolder Class .

  • The roaming storage :

    Has the same fearture than The Local Storage adding to that synchronization Data with windows Devices , identified by RoamingSettings Class.

  • The temporary storage :

    As his name indicates It's used mainly for caching scenarios .

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