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?

manas
  • 399
  • 6
  • 24
level_zebra
  • 1,373
  • 6
  • 22
  • 41

5 Answers5

2

try

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

msdn

Jarek Kardas
  • 8,425
  • 1
  • 30
  • 32
0

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

rt2800
  • 3,015
  • 2
  • 18
  • 26
0

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

Tomtom
  • 8,699
  • 7
  • 48
  • 87
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.

Nickon
  • 9,028
  • 10
  • 58
  • 110
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();
jomsk1e
  • 3,514
  • 7
  • 33
  • 57