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

标签 c# visual-studio-2015 windows-10 win-universal-app

我正在 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: 

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

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

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

VS Solution Explorer

我已经检查过该文件是否存在(在 VS 的属性 View 中复制设置为“如果较新则复制”)并且权限是这样的,我的用户帐户可以访问它(读/写/执行)。我什至尝试以管理员权限打开 VS,但没有成功。

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

projectdates.xml properties

编辑 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: 

最佳答案

您看到的异常可能是由于 Windows 通用应用程序中的沙盒造成的。但是,File.Open 的文档没有提到它。

通常在 Windows 运行时应用程序中,您必须使用 StorageFile而不是文件流:

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# - UWP - 项目中包含的文件出现 UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32530370/

相关文章:

c# - 如何使用 VS2010 安装项目将我的程序添加到用户开始菜单?

c# - 如何在 C# 中删除或替换 "\"

c# - 找不到数据库文件

c# - 是什么让 Visual Studio 调试器停止评估 ToString 覆盖?

c# - Xamarin Studio(在 OSX 上)还是 Visual Studio(在 Windows 上)?

窗口/命令 |如果例如,如何断线字符数达到?

c# - ASP.NET:带有自定义用户表的自定义 MembershipProvider

c# - 共享目标 Universal Apps Windows 10 方法

c# - 如何在 C# 应用程序中获得一致的读/写应用程序路径?

c# - ASP.NET MVC 5 和 Visual Studio 2015RC CS0234 和 CS0103 错误