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

c# - UWP - UnauthorizedAccessException on file included in project

我正在 Visual Studio 2015 社区编写一个 UWP 应用程序,当我 运行 构建和应用程序尝试打开我使用 [ 添加到项目中的文件时出现以下异常=32=]项目 |从 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: 

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

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

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

我已经检查过该文件是否存在(在 VS 的属性视图中复制设置为 "Copy if newer")并且我的用户帐户可以访问它的权限 (read/write/execute)。我什至尝试以管理员权限打开 VS,但没有成功。

编辑: 以下是 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: 

将文件的构建操作更改为内容。

对于 XML 和 LINQ:

var path =  Path.Combine(
            Windows.ApplicationModel.Package.Current.InstalledLocation.Path, 
            "projectdates.xml");
XDocument data = XDocument.Load(path);

Also relevant

听起来像是一条格式错误的路径..."c:...\documents" 真的存在吗?如果没有,请尝试像这样获取路径:

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\visual studio 2015\Projects\ProjectManager\ProjectManager\bin\x86\Debug\AppX\projectdates.xml"

您看到的异常可能是由于 Windows 通用应用程序中的沙盒。但是,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);
    ...
}