2017-07-13 45 views
0

我知道在看似相同的问题上存在一堆线程,但我不能在3小时后解决这个问题,所以我真的需要一些帮助。System.UnauthorizedAccessException C#Windows 10手机模拟器

据我所知,我得到这个错误,因为系统无权访问该文件。我已经尝试将权限设置为全部和其他几个代码片段来解决我的问题,但没有任何工作。

这是使用Xaramin,

我试图填充从XML文件联系人列表框在Windows 10应用程序。我有列表框itemsSource设置为“数据上下文”和路径“myList”。 XML构建操作设置为“内容”,并将“复制到输出目录”设置为“始终复制”。

我试图按照从我的课程从初学者的教程3次,总是得到相同的错误。

这里是我得到enter image description here

错误下面是页面上的全部代码。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using System.Xml; 
using System.Xml.Linq; 
using Windows.Storage; 
using System.Collections.ObjectModel; 

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 

namespace ContactsApp 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page 
    { 
     string TEMPFILEPATH = ""; 
     string TARGETFILEPATH = ""; 
     private ObservableCollection<string> lstd = new ObservableCollection<string>(); 
     public ObservableCollection<string> myList { get { return lstd; } } 
     public MainPage() 
     { 
      this.InitializeComponent(); 
     } 

     private void Grid_Loading(FrameworkElement sender, object args) 
     { 
      Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current; 
      StorageFolder installedLocation = package.InstalledLocation; 
      StorageFolder targetLocation = ApplicationData.Current.LocalFolder; 

      TEMPFILEPATH = installedLocation.Path.ToString() + "\\Contacts.xml"; 
      TARGETFILEPATH = targetLocation.Path.ToString() + "\\Contacts.xml"; 
      File.Move(TEMPFILEPATH, TARGETFILEPATH); 
      loadContacts(); 
     } 
     private void loadContacts() 
     { 
      XmlReader xmlReader = XmlReader.Create(TARGETFILEPATH); 
      while (xmlReader.Read()) 
      { 
       if (xmlReader.Name.Equals("ID") && (xmlReader.NodeType == XmlNodeType.Element)) 
       { 
        lstd.Add(xmlReader.ReadElementContentAsString()); 
       } 
      } 
      DataContext = this; 
      xmlReader.Dispose(); 
     } 
    } 
} 

我将永远感谢您对此事的任何帮助。 :)

回答

2

您不应尝试访问受限环境(如Windows Phone)中的受限路径。

相反,如果你真的需要嵌入与您的应用程序文件,更改构建行动Embedded ResourceDo not copy为XML文件,然后检索您的代码中的资源作为嵌入资源:

public void LoadContacts() 
{ 
    const string fileName = "Contacts.xml"; 

    var assembly = typeof(MainPage).GetTypeInfo().Assembly; 

    var path = assembly.GetManifestResourceNames() 
     .FirstOrDefault(n => n.EndsWith(fileName, StringComparison.OrdinalIgnoreCase)); 

    if(path == null) 
     throw new Exception("File not found"); 

    using (var stream = assembly.GetManifestResourceStream(path)) 
    using (var reader = XmlReader.Create(stream)) 
    {   
     while (reader.Read()) 
     { 
      if (reader.Name.Equals("ID") && (reader.NodeType == XmlNodeType.Element)) 
      { 
       lstd.Add(reader.ReadElementContentAsString()); 
      } 
     } 
    } 

    DataContext = this; // better to move this inside the constructor 
} 
+0

你好,队友欢呼堡回答。我做了你所说的,现在我的代码看起来像这个https://pastebin.com/sXS4262y。我收到第49行的错误。类型不包含程序集,也没有扩展名,组件接受类型'Type'的第一个参数。 我尝试使用system.reflection添加,但没有奏效。 我也改变了xml文件设置,如你所说。 – Rick1990

+0

忘记添加UWP中需要的'GetTypeInfo()'。编辑答案。 –

+0

谢谢你这个工作 – Rick1990