c# - UWP - 项目中包含的文件上的UnauthorizedAccessException

GHa*_*del 5 c# win-universal-app windows-10 visual-studio-2015

我正在Visual Studio 2015社区中编写UWP应用程序,并在运行构建时遇到以下异常,并且应用程序尝试打开我使用Project添加到项目中的文件.从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: 
Run Code Online (Sandbox Code Playgroud)

这是试图访问该文件的代码片段:

XmlReader xml = XmlReader.Create(
    File.Open("projectdates.xml", FileMode.Open, FileAccess.Read)
);
Run Code Online (Sandbox Code Playgroud)

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

VS Solution Explorer

我已经检查过该文件是否存在(复制设置是VS的属性视图中的"如果更新则复制")并且权限是我的用户帐户可以访问它(读/写/执行).我甚至尝试用管理员权限打开VS并且没有占上风.

编辑: 以下是接收异常之前XML的属性:

projectdates.xml属性

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

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: 
Run Code Online (Sandbox Code Playgroud)

chu*_*e x 12

您看到的例外可能是由于Windows Universal Apps中的沙盒.但是,File.Open的文档没有提到它.

通常在Windows运行时应用程序中,您必须使用StorageFile而不是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);
    ...
}
Run Code Online (Sandbox Code Playgroud)