我有一個名爲settings.xml文件位於:獲取文件的相對路徑?
c:\solution1\solution1\data\settings.xml
現在,我做的:
XDocument doc = XDocument.Load(@"c:\solution1\solution1\settings.xml");
我想不出如何使用相對路徑做。
我有一個名爲settings.xml文件位於:獲取文件的相對路徑?
c:\solution1\solution1\data\settings.xml
現在,我做的:
XDocument doc = XDocument.Load(@"c:\solution1\solution1\settings.xml");
我想不出如何使用相對路徑做。
如果你的意思是相對於你的可執行文件,您可以使用
string exeLocation = System.Reflection.Assembly.GetExecutingAssembly().CodeBase
注意經常建議
System.Reflection.Assembly.GetExecutingAssembly().Location
將獲得其中的組件目前位於的路徑,它可以是不同的例如如果陰影副本正在執行。
您可以使用
string exeDir = System.IO.Path.GetDirectoryName(exeLocation);
獲得可執行文件的目錄。
如果你想找到一個文件,是在數據目錄中的安裝位置下方,你可以做
string dataFile = Path.Combine(exeDir, "data\settings.xml");
需要注意的是在Windows Vista和更高版本,你不會有寫訪問默認爲安裝目錄下的目錄。
開始見http://stackoverflow.com/questions/362790/what-is-the-best-way-to-determine-應用程序根目錄 – abatishchev