2016-06-16 59 views
3

我已經創建了一個WPF項目,並在我的解決方案中添加了一個「ResFiles」文件夾。在這個文件夾裏面我放了幾個XML文件。在代碼中,爲了訪問這些文件,我使用瞭如下所述的相對路徑。解決方案文件夾路徑問題內的本地文件問題

using (StreamWriter writer = new StreamWriter(@"../../ResFiles/xyz.xml")) 
     { 
      serializer.Serialize(writer, mydata, ns); 
     } 

這工作正常。但是,當我爲我的應用程序創建安裝項目時,文件夾結構正在改變,並且所給出的路徑不起作用。這是拋出文件沒有發現異常。如何解決這個路徑問題?

+0

a使用Environment.CurrentDirectory作爲基礎? – smoksnes

+0

在解決方案中,我有MySolution-> MyProject-> ResFiles-> xyz.xml路徑。當我安裝應用程序時,路徑是MyProject-> ResFiles-> xyz.xml。 –

回答

2

你可以試試這個

var f = File.ReadAllText(System.IO.Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + @"/Files/file.txt"); 

,你也需要改變生成操作,並複製到輸出目錄enter image description here

0

你可以試試這些

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) 

Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"ResFiles\xyz.xml")