我有一个名为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