简体   繁体   English

查找xml配置文件

[英]Finding a xml config file

I am trying to check whether an xml config file exists. 我试图检查是否存在xml配置文件。

The file has the name MyApp.exe.config 该文件的名称为MyApp.exe.config

I am using 我在用

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

this returns false despite the file existing 尽管文件存在,但它返回false

Can anyone advise on the correct way of checking whether or not this file exists? 任何人都可以建议正确的方法来检查这个文件是否存在?

try 尝试

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

msdn MSDN

at runtime Directory.GetCurrentDirectory() will return debug/release/bin dir path. 在运行时Directory.GetCurrentDirectory()将返回debug / release / bin目录路径。 Does the config XML file reside in those folders? 配置XML文件是否位于这些文件夹中?

You can just check for File.Exists(CONFIGFILENAME). 您可以检查File.Exists(CONFIGFILENAME)。 Because .net takes the relativ path from the current directory 因为.net从当前目录获取相对路径

For first I recommend you to use Path.Combine(path, fileName); 首先,我建议你使用Path.Combine(path, fileName); to create paths. 创造路径。

For second use Application.StartupPath , not Directory.GetCurrentDirectory . 第二次使用Application.StartupPath ,而不是Directory.GetCurrentDirectory

For third, make sure that your application folder contains MyApp.exe.config . 第三,确保您的应用程序文件夹包含MyApp.exe.config

Please consider this method: 请考虑这种方法:

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

I think the culprit on your method is this: 我认为你方法的罪魁祸首是:

filepath = Directory.GetCurrentDirectory();

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

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