I'm writing a UWP app in Visual Studio 2015 community and getting the following exception when I run the build and the app tries to open a file that I added to the project using Project | Add New Item from 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:
and here's the code snippet that is trying to access the file:
XmlReader xml = XmlReader.Create(
File.Open("projectdates.xml", FileMode.Open, FileAccess.Read)
);
lastly, here's a screenshot of my solution tree:
I've already checked that the file exists (Copy setting is "Copy if newer" in properties view of VS) and that the permissions are such that my user account can access it (read/write/execute). I've even trying opening VS with admin rights and to no prevail.
Edit: Here are the properties for the XML prior to receiving the exception:
Edit 2:
Alright, I've tried using the StorageFile
method of accessing the file and now I'm getting this new exception:
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:
The exception you are seeing is likely due to the sandboxing in Windows Universal Apps. However, the documentation for File.Open does not mention it.
Typically in Windows Runtime apps, you have to use StorageFile instead of 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);
...
}
Sounds like a malformed path...does "c:...\documents" really exist? if not, try getting the path like so:
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\visual studio 2015\Projects\ProjectManager\ProjectManager\bin\x86\Debug\AppX\projectdates.xml"
Change your file’s Build Action to Content.
For XML with LINQ:
var path = Path.Combine(
Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
"projectdates.xml");
XDocument data = XDocument.Load(path);
Also relevant
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With