简体   繁体   English

UWP App不断抛出未经授权的访问异常

[英]UWP App Keeps Throwing Unauthorized Access Exception

I am making an UWP App and I keep having a System.UnauthorizedAccessException 我正在制作一个UWP应用,并且我一直拥有System.UnauthorizedAccessException

Here is the full Code of my main file 这是我主文件的完整代码

private void SaveButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var user = new Configuration();
            string Email = txtEmail.Text.Trim();

                user[Email]["Value"].StringValue = "test";

            string dest = Path.GetFullPath(@"C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\bin\x86\Debug\AppX\Test\user.cfg");
            user.SaveToFile(dest);
        }

As I was reading the documentation for File Access Permission for UWP apps, it had suggested to add this line 当我阅读UWP应用程序的文件访问权限文档时,它建议添加此行

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap5 rescap"> ... xmlns:rescap =“ http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities” IgnorableNamespaces =“ uap mp uap5 rescap”> ...

And this is my AppxManifest file 这是我的AppxManifest文件

<Package xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap5 rescap" xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5">
---
  <Capabilities>
    <rescap:Capability Name="broadFileSystemAccess" />
  </Capabilities>

So when I try to save a file. 因此,当我尝试保存文件时。 I still keep having this error. 我仍然继续出现此错误。

System.UnauthorizedAccessException HResult=0x80070005 System.UnauthorizedAccessException HResult = 0x80070005
Message=Access to the path 'C:\\Users\\IT\\Desktop\\AttendanceApp\\AttendanceApp\\bin\\x86\\Debug\\AppX\\Test\\user.cfg' is denied. 消息=拒绝访问路径'C:\\ Users \\ IT \\ Desktop \\ AttendanceApp \\ AttendanceApp \\ bin \\ x86 \\ Debug \\ AppX \\ Test \\ user.cfg'。 Source=System.Private.CoreLib StackTrace: at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) Source = System.Private.CoreLib StackTrace:位于System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at SharpConfig.Configuration.SaveToFile(String filename, Encoding encoding) at SharpConfig.Configuration.SaveToFile(String filename) at AttendanceApp.MainPage.SaveButton_Click(Object sender, RoutedEventArgs e) in C:\\Users\\IT\\Desktop\\AttendanceApp\\AttendanceApp\\MainPage.xaml.cs:line 57 在SharpConfig.Configuration的System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项)的System.IO.FileStream.CreateFileOpenHandle(FileMode模式,FileShare共享,FileOptions选项)在C:\\ Users \\ IT \\ Desktop \\ AttendanceApp \\ AttendanceApp \\ Main.xaml中的AttendanceApp.MainPage.SaveButton_Click(Object sender,RoutedEventArgs e)中的SharpConfig.Configuration.SaveToFile(String filename)中的.SaveToFile(字符串文件名,编码编码)。 cs:第57行

By the way I am also using SharpConfig to edit my .cfg files 顺便说一句,我也使用SharpConfig编辑我的.cfg文件

Any ideas why this happens? 任何想法为什么会发生这种情况? Any help would be great 任何帮助都会很棒

There are two places you need to be aware of: 您需要注意两个地方:

  1. As @mm8 said, the path in your above code C:\\Users\\IT\\Desktop\\AttendanceApp\\AttendanceApp\\bin\\x86\\Debug\\AppX\\ equals Windows.ApplicationModel.Package.Current.InstalledLocation.Path . 如@ mm8所述,您上面的代码C:\\Users\\IT\\Desktop\\AttendanceApp\\AttendanceApp\\bin\\x86\\Debug\\AppX\\等于Windows.ApplicationModel.Package.Current.InstalledLocation.Path In UWP, the installed localtion folder is read-only. 在UWP中,已安装的语言环境文件夹是只读的。 You cannot use any APIs to write in it. 您不能使用任何API编写代码。 You could think about coping 'user.cfg' to other place, then you could write to it. 您可以考虑将“ user.cfg”处理到其他位置,然后可以写它。 The ApplicationData folder would be a good choice. ApplicationData文件夹将是一个不错的选择。 For example, LocalFolder . 例如, LocalFolder
  2. I saw that you add the broadFileSystemAccess capability to access files outside the app cantainer. 我看到您添加了broadFileSystemAccess功能来访问应用程序容器之外的文件。 That's great, but you missed an important prompt 'This capability works for APIs in the Windows.Storage namespace.' 很好,但是您错过了一个重要提示: “此功能适用于Windows.Storage命名空间中的API。” . I've checked the SharpConfig's source code, the SaveToFile method uses the 'FileStream' relevant APIs to write file. 我已经检查了SharpConfig的源代码, SaveToFile方法使用与“ FileStream”相关的API来写入文件。 It's not included in 'Windows.Storage Namespace'. 它不包含在“ Windows.Storage命名空间”中。 So, the 'broadFileSystemAccess' capanility doesn't work for it. 因此,“ broadFileSystemAccess”功能不起作用。

So, if you have to use 'SharpConfig' in your project, you need to use the 'Windows.Storage Namespace' APIs to change its source code and compile a custom version for your UWP project. 因此,如果必须在项目中使用“ SharpConfig”,则需要使用“ Windows.Storage命名空间” API更改其源代码并为UWP项目编译自定义版本。 In the meantime, keep in mind my explanation in the first paragraph. 同时,请记住我在第一段中的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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