Opening File in C# Access Denied [duplicate]

拜拜、爱过 提交于 2019-12-20 06:27:46

问题


I am trying to read the content of a text file but I get the 'Access to the path is denied' exception of type 'System.UnauthorizedAccessException'. I've tried the following:

  • Ran Vs in Administrator mode
  • Checked if the file is read only
  • Checked if the file is hidden
  • Checked if the file has full control on all users

My code:

 private async void MyButton_Click(object sender, RoutedEventArgs e)
    {
        string path = @"fullpath\TextFile.txt";
        await Task.Run(() =>
       {
           string text = File.ReadAllText(path);
       });
    }

回答1:


You can't access all your files just like that in Windows Store apps. Please take a look at this MSDN page for list of locations, which your app is permited to use. Also to use most of them you will have to declare suitable Capabilities. If your file is outside this list and/or you haven't declared capabilities, you will get UnauthorizedException.

Generaly store apps shouldn't access files without user knowing about it - this is by design. If you want to access file in future, you can for example pick a file with FileOpenPicker and then access it by FutureAccessList or MostRecentlyUsed.



来源:https://stackoverflow.com/questions/35434692/opening-file-in-c-sharp-access-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!