繁体   English   中英

查找xml配置文件

[英]Finding a xml config file

我试图检查是否存在xml配置文件。

该文件的名称为MyApp.exe.config

我在用

public static bool FileExistsCheck(string filename, string filepath = "")
{
    if (filepath.Length == 0)
        filepath = Directory.GetCurrentDirectory();
    return File.Exists(filepath + "\\" + filename);
}

尽管文件存在,但它返回false

任何人都可以建议正确的方法来检查这个文件是否存在?

尝试

return File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)

MSDN

在运行时Directory.GetCurrentDirectory()将返回debug / release / bin目录路径。 配置XML文件是否位于这些文件夹中?

您可以检查File.Exists(CONFIGFILENAME)。 因为.net从当前目录获取相对路径

首先,我建议你使用Path.Combine(path, fileName); 创造路径。

第二次使用Application.StartupPath ,而不是Directory.GetCurrentDirectory

第三,确保您的应用程序文件夹包含MyApp.exe.config

请考虑这种方法:

public static bool isFileExist(string filePath)
{
    if (File.Exists(filePath))
        return true;
    else return false;
}

我认为你方法的罪魁祸首是:

filepath = Directory.GetCurrentDirectory();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM