C#如何从当前目录加载XML文件?

5

我在我的C# Winform应用程序中使用以下代码行,这很好用,但偶尔如果程序从命令行运行,会出现找不到config.xml文件的错误。这是因为“工作目录”不同(我想),我需要说“从当前目录加载config.xml”,我该怎么做?

docXML.Load("config.xml");

谢谢Jonathan

2个回答

8
string fileName = Path.Combine(Application.StartupPath, "config.xml");

3
    string Path = "";
    string Filename = ConfigurationManager.AppSettings("Filename");

用于从当前目录加载

   Path = System.Web.HttpContext.Current.Server.MapPath(Filename);   

用于从基本目录加载

    Path = AppDomain.CurrentDomain.BaseDirectory + Filename;        

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接