0

I am trying to check whether an xml config file exists.

The file has the name 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

Can anyone advise on the correct way of checking whether or not this file exists?

3

5 Answers 5

2

try

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

msdn

0

at runtime Directory.GetCurrentDirectory() will return debug/release/bin dir path. Does the config XML file reside in those folders?

0

You can just check for File.Exists(CONFIGFILENAME). Because .net takes the relativ path from the current directory

0

For first I recommend you to use Path.Combine(path, fileName); to create paths.

For second use Application.StartupPath, not Directory.GetCurrentDirectory.

For third, make sure that your application folder contains MyApp.exe.config.

0

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();

Not the answer you're looking for? Browse other questions tagged or ask your own question.