Reputation: 1533
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
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
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
Reputation: 9394
You can just check for File.Exists(CONFIGFILENAME). Because .net takes the relativ path from the current directory
Upvotes: 0
Reputation: 8455
try
return File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
Upvotes: 2
Reputation: 3045
at runtime Directory.GetCurrentDirectory()
will return debug/release/bin dir path. Does the config XML file reside in those folders?
Upvotes: 0