C# - 包含在项目上的文件UnauthorizedAccessException - UWP [英] c# - UWP - UnauthorizedAccessException on file included in project

查看:2543
本文介绍了C# - 包含在项目上的文件UnauthorizedAccessException - UWP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio中编写一个应用程序UWP 2015年社区并得到以下异常,当我运行生成和应用程序试图打开我使用的项目添加到项目中的文件|添加新项从VS.

  System.UnauthorizedAccessException的是由用户代码
HResult的= -2147024891
消息=访问路径未处理C:\Users\garre\documents\visual工作室2015\Projects\ProjectManager\ProjectManager\bin\x86\Debug\AppX\projectdates.xml'被拒绝。
来源= ProjectManager
堆栈跟踪:$在ProjectManager.DataController B $ B< CheckDates> d__5.MoveNext()
---从以前的位置堆栈跟踪结束在那里异常被抛出 - -
在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter `1.GetResult()
在ProjectManager.MainPage<< projectsPivot_Click> b__21_0> d.MoveNext()
---从以前的位置堆栈跟踪的结束,其中引发异常---
。在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter.GetResult( )
在ProjectManager.MainPage< projectsPivot_Click> d__21.MoveNext()
的InnerException:

和这里的正试图访问该文件的代码片段:

 的XmlReader XML = XmlReader.Create(
File.Open(projectdates.xml,FileMode.Open,FileAccess.Read)
);



最后,这里是我的解决方案树的截图:





我已经检查文件是否存在(复制设置复制,如果新,在属性中查看VS的),并且权限是这样的,我的用户帐户可以访问(读/写/执行)。 。我甚至尝试开放VS拥有管理员权限,并没有占上风



编辑:
下面是在XML属性之前收到异常:





编辑2:
好​​吧,我一直在使用访问该文件的 StorageFile 方法尝试,现在我得到这个新的异常:

  System.UnauthorizedAccessException的是由用户代码
HResult的= -2147024891
消息=访问被拒绝未处理。 (异常来自HRESULT:0X80070005(E_ACCESSDENIED))
来源= mscorlib程序
堆栈跟踪:
在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)$在System.Runtime.CompilerServices.TaskAwaiter.GetResult()
在ProjectManager.DataController< b $ b:。的GetData> d__6.MoveNext()
---从以前的位置堆栈跟踪,其中引发异常---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
结束
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()LT
在ProjectManager.MainPage&;< projectsPivot_Load> b__21_0>栈d.MoveNext()
---结束从以前的位置,引发异常---
跟踪的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
。在System.Runtime.CompilerServices.TaskAwaiter.GetResult()
在ProjectManager.MainPage< projectsPivot_Load> d__21.MoveNext()
的InnerException:


解决方案

您所看到的例外可能是由于在Windows应用程序的通用沙盒。然而,对于 File.Open 的文件没有提到它。



通常,在Windows运行时的应用程序,你必须使用的 StorageFile 代替的FileStream:

 使用System.IO;使用System.Threading.Tasks 
;
使用的System.Xml;
使用Windows.Storage;
使用Windows.ApplicationModel;以下行的

异步公共任务ReadXmlFile()
{
//之一来获取文件:
VAR SF =等待Package.Current.InstalledLocation。 TryGetItemAsync(projectdates.xml)作为StorageFile;
VAR SF =等待StorageFile.GetFileFromApplicationUriAsync(新的URI(@MS-APPX:///projectdates.xml));

VAR流=等待sf.OpenStreamForReadAsync();
的XmlReader XML = XmlReader.Create(流);

}


I'm writing a UWP app in Visual Studio 2015 community and getting the following exception when I run the build and the app tries to open a file that I added to the project using Project | Add New Item from VS.

System.UnauthorizedAccessException was unhandled by user code
  HResult=-2147024891
  Message=Access to the path 'C:\Users\garre\documents\visual studio 2015\Projects\ProjectManager\ProjectManager\bin\x86\Debug\AppX\projectdates.xml' is denied.
  Source=ProjectManager
  StackTrace:
       at ProjectManager.DataController.<CheckDates>d__5.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at ProjectManager.MainPage.<<projectsPivot_Click>b__21_0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at ProjectManager.MainPage.<projectsPivot_Click>d__21.MoveNext()
  InnerException: 

and here's the code snippet that is trying to access the file:

XmlReader xml = XmlReader.Create(
    File.Open("projectdates.xml", FileMode.Open, FileAccess.Read)
);

lastly, here's a screenshot of my solution tree:

I've already checked that the file exists (Copy setting is "Copy if newer" in properties view of VS) and that the permissions are such that my user account can access it (read/write/execute). I've even trying opening VS with admin rights and to no prevail.

Edit: Here are the properties for the XML prior to receiving the exception:

Edit 2: Alright, I've tried using the StorageFile method of accessing the file and now I'm getting this new exception:

System.UnauthorizedAccessException was unhandled by user code
  HResult=-2147024891
  Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
  Source=mscorlib
  StackTrace:
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at ProjectManager.DataController.<GetData>d__6.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at ProjectManager.MainPage.<<projectsPivot_Load>b__21_0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at ProjectManager.MainPage.<projectsPivot_Load>d__21.MoveNext()
  InnerException: 

解决方案

The exception you are seeing is likely due to the sandboxing in Windows Universal Apps. However, the documentation for File.Open does not mention it.

Typically in Windows Runtime apps, you have to use StorageFile instead of FileStream:

using System.IO;
using System.Threading.Tasks;
using System.Xml;
using Windows.Storage;
using Windows.ApplicationModel;

async public Task ReadXmlFile()
{
    // Use ONE of the following lines to get the file:
    var sf = await Package.Current.InstalledLocation.TryGetItemAsync("projectdates.xml") as StorageFile;
    var sf = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///projectdates.xml"));

    var stream = await sf.OpenStreamForReadAsync();
    XmlReader xml = XmlReader.Create(stream);
    ...
}

这篇关于C# - 包含在项目上的文件UnauthorizedAccessException - UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆