2017-07-13 100 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