2012-11-09 42 views
3

我正在構建Windows Phone 8應用程序。 在設計時我加載一個示例XML文件來獲取示例數據。 它工作的很好,但我想使用與解決方案根目錄相關的文件路徑,因此它可以用於具有相同代碼的所有開發人員。XAML:在設計時獲取文件的相對路徑

這裏是我當前的代碼:

var path = @"C:\Users\Tom\MyProject\SampleData\stub.xml"; 
xml = new StreamReader(path).ReadToEnd(); 

我試着像@ 「的sampleData \ stub.xml」 的相對路徑。它適用於手機,但在設計時我得到這個錯誤:

DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\SampleData\login.xml'. 
+1

的相對路徑已經具備一定的基礎,涉及到。我想這是你的工作方向。 ;-) 將文件包含在您的項目中並設置操作:複製如果更新 –

+0

這不完全相同的問題,但基本上你需要使用我認爲這個問題的解決方案之一:http://stackoverflow.com/questions/2425944/how-to-get-the-project-path-at-design-time –

+0

感謝Dana,這是我工作的唯一解決方案:http://stackoverflow.com/questions/723172/project-濱文件夾路徑,在編譯時 –

回答

1

這裏是我做過什麼:

var path = "SampleData/stub.xml"; 
var res = Application.GetResourceStream(new Uri(path, UriKind.Relative)) 
xml = new StreamReader(res.Stream).ReadToEnd();