我正在 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 的属性 View 中复制设置为“如果较新则复制”)并且权限是这样的,我的用户帐户可以访问它(读/写/执行)。我什至尝试以管理员权限打开 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:
最佳答案
您看到的异常可能是由于 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/