Tob*_*and 9 .net c# xml path visual-studio-2012
我正在尝试加载位于项目文件夹中的XML文件(使用Visual Studio 2012).
结构是这样的:
solutionRoot\
-  service\
--   ServiceClass.cs
--   AppValues.xml  <-- this is the file I want to load
在我的ServiceClass中,我尝试使用以下代码从XML文件中读取:
public String GetXmlElement(String elementName)
{
    [....]
    XDocument document = XDocument.Load(@"\service\AppValues.xml");
    [...]
}
当我尝试测试代码时,会出现以下错误:
Test method PandaTests.ServiceTest.ReadXmlCanReadXml threw exception: 
System.IO.DirectoryNotFoundException: Could not find a part of the path 
'C:\Users\MyName\Documents\GitHub\project\Project22\PandaTests\bin\Debug\service\AppValues.xml'.
这显然是我的路径的问题,但我无法弄清楚如何使相对路径正确.我在这里查看了有关堆栈溢出的其他问题,但其中许多似乎过度参与.是否有一种简单的方法来加载XML文件而不提供绝对路径?
Tyl*_*Lee 14
当VS运行您的程序时,您的工作目录将设置为Debug/Release文件夹,而不是您的解决方案根目录.
你知道几个选项......
| 归档时间: | 
 | 
| 查看次数: | 45546 次 | 
| 最近记录: |