level_zebra
level_zebra

Reputation: 1533

Finding a xml config file

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?

Upvotes: 0

Views: 268

Answers (5)

jomsk1e
jomsk1e

Reputation: 3625

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

Upvotes: 0

Nickon
Nickon

Reputation: 10156

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.

Upvotes: 0

Tomtom
Tomtom

Reputation: 9394

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

Upvotes: 0

Jarek Kardas
Jarek Kardas

Reputation: 8455

try

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

msdn

Upvotes: 2

rt2800
rt2800

Reputation: 3045

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

Upvotes: 0

Related Questions