我在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)
);
最后,这是我的解决方案树的屏幕截图:
我已经检查过该文件是否存在(复制设置为&#34;如果更新则复制&#34;在VS的属性视图中),并且权限是我的用户帐户可以访问它(读取/写/执行)。我甚至尝试用管理员权限打开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:
答案 0 :(得分: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);
...
}
答案 1 :(得分:1)
将文件的构建操作更改为内容。
对于使用LINQ的XML:
var path = Path.Combine(
Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
"projectdates.xml");
XDocument data = XDocument.Load(path);
答案 2 :(得分:1)
听起来像一条畸形的路径......“c:...... \ documents”真的存在吗?如果没有,请尝试这样的路径:
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\visual studio 2015\Projects\ProjectManager\ProjectManager\bin\x86\Debug\AppX\projectdates.xml"