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
  • 397
  • 6
  • 25
level_zebra
  • 1,503
  • 6
  • 25
  • 44

5 Answers5

2

try

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

msdn

Jarek Kardas
  • 8,445
  • 1
  • 31
  • 32
0

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

rt2800
  • 3,045
  • 2
  • 19
  • 26
0

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

Tomtom
  • 9,087
  • 7
  • 52
  • 95
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,652
  • 12
  • 64
  • 119
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,585
  • 7
  • 34
  • 59