Visual studio 2017 在Visual Studio 2017中加载动态生成的XML文件的最佳选项是什么
我有一个类库项目,它读取xml文件。xml文件将随时动态更新。因此,我并没有将VisualStudio中的xml文件设置为Build Action=Embedded Resource,因为每次都需要编译它 现在,如果我通过获取相对路径将其作为外部源读取。以下是我用来读取文件的代码:Visual studio 2017 在Visual Studio 2017中加载动态生成的XML文件的最佳选项是什么,visual-studio-2017,relative-path,absolute-path,file-read,Visual Studio 2017,Relative Path,Absolute Path,File Read,我有一个类库项目,它读取xml文件。xml文件将随时动态更新。因此,我并没有将VisualStudio中的xml文件设置为Build Action=Embedded Resource,因为每次都需要编译它 现在,如果我通过获取相对路径将其作为外部源读取。以下是我用来读取文件的代码: public static string GetDatabaseXMLPath() { string LPath = string.Empty; try { string Lo
public static string GetDatabaseXMLPath()
{
string LPath = string.Empty;
try
{
string Location = AppDomain.CurrentDomain.BaseDirectory;
int index;
int len;
index = Location.IndexOf("MyProject");
len = Location.Length;
if (index > 0)
{
LPath = Location.Remove(index, len - index);
LPath = LPath + "MyProject\\DatabaseInfoFile\\Database.xml";
}
else
{
LPath = Location;
}
Console.WriteLine(LPath);
}
catch (Exception e)
{
Console.WriteLine("Error in GetDatabaseXMLPath " + e);
}
return LPath;
}
上面的代码运行良好,我可以通过同一解决方案中的控制台测试项目来阅读。
问题是,当我用这个Dll创建一个Nuget包并在另一个项目(比如project-2)中使用它时。
另一个Project-2thows错误就好像在它自己的bin目录而不是Dll目录中查找文件一样
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\My Projects\Project-2\Project-2\bin\Debug\'.
到目前为止,只有在Dll中使用绝对路径时,它才能工作
const string FILENAME = @"\FileName\Database.xml";
并将其解读为:
XDocument doc = XDocument.Load(FILENAME);
dll将放置在服务器(dev/uat/prod)上,因此请告知是否有更好的解决方案