0

我正在尝试检查是否存在 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尽管文件存在,但仍会返回

谁能建议检查此文件是否存在的正确方法?

4

5 回答 5

2

尝试

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

微软

于 2012-10-11T11:53:28.477 回答
0

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

于 2012-10-11T11:53:25.097 回答
0

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

于 2012-10-11T11:54:45.853 回答
0

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

二次使用Application.StartupPath,不行Directory.GetCurrentDirectory

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

于 2012-10-11T11:54:54.240 回答
0

请考虑这种方法:

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

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

filepath = Directory.GetCurrentDirectory();
于 2012-10-11T12:43:37.443 回答