C# 查找xml配置文件
我正在尝试检查xml配置文件是否存在 该文件的名称为MyApp.exe.config 我正在使用C# 查找xml配置文件,c#,filepath,file-exists,C#,Filepath,File Exists,我正在尝试检查xml配置文件是否存在 该文件的名称为MyApp.exe.config 我正在使用 public static bool FileExistsCheck(string filename, string filepath = "") { if (filepath.Length == 0) filepath = Directory.GetCurrentDirectory(); return File.Exists(filepath + "\\" + fi
public static bool FileExistsCheck(string filename, string filepath = "")
{
if (filepath.Length == 0)
filepath = Directory.GetCurrentDirectory();
return File.Exists(filepath + "\\" + filename);
}
不管文件是否存在,这都会返回false
是否有人可以建议检查此文件是否存在的正确方法?在运行时
目录。GetCurrentDirectory()
将返回debug/release/bin dir路径。配置XML文件是否驻留在这些文件夹中?试试看
return File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
您只需检查File.Exists(配置文件名)。由于.net首先从当前目录中获取相对路径,因此我建议您使用
path.Combine(path,fileName)代码>以创建路径
第二次使用应用程序.StartupPath
,而不是目录.GetCurrentDirectory
第三,确保您的应用程序文件夹包含<代码> MyApp.exe .CONFIG .< /P> < P>请考虑此方法:
public static bool isFileExist(string filePath)
{
if (File.Exists(filePath))
return true;
else return false;
}
我认为你的方法的罪魁祸首是:
filepath = Directory.GetCurrentDirectory();
你为什么要知道?也许有另一种方法可以实现你想要的。