C # - UWP - UnauthorizedAccessException on a file included in a project

I write a UWP application in the Visual Studio 2015 community and get the following exception when starting the build, and the application tries to open the file that I added to the project using Project | Add a 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 is a piece of code trying to access the file:

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

Finally, here is a screenshot of my decision tree:

VS Solution Explorer

I already checked that the file exists (the copy option "Copy if new" in the VS properties view) and that the permissions are such that my user account can access it (read / write / execute). I even try to open VS with administrator privileges and not prevail.

Edit: Here are the properties for XML until an exception is thrown:

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: 
+4
3

, , , Windows Universal Apps. File.Open .

, Runtime 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);
    ...
}
+12

. .

XML LINQ:

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

+1

... "c:...\documents"? , :

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

Source: https://habr.com/ru/post/1607013/


All Articles