In a C# 2010 desktop application, I have the following code:
string Format_Date = DateTime.Now.AddMonths(-1).ToString("MM-yyyy");
string filesaveLocation = ConfigurationSettings.AppSettings["Location"] + "\\" + Format_Date + "\\";
if (Directory.Exists(filesaveLocation))
When the value is displayed from filesaveLocation the value: "C:\\\\Trans\\12-2012\\".
The problem is once I get to the following line of code:
if (Directory.Exists(filesaveLocation)) the same is always true.
It does not make any difference if I say (!Directory.Exists(filesaveLocation)).
This answer is true also.
Thus can you show me in code how to correct this problem?
Loading

VulpesRank #55 · 29.2K reputationPosted Jan 29, 2013, 5:16 AM EST
VulpesRank #55 · 29.2K reputationPosted Jan 28, 2013, 10:04 AM EST
In this situation, Directory.Exists should always return false and !Directory.Exists should therefore return true.
It appears that there is a mistake in Configuration.AppSettings which is causing this problem.
I'd either correct it in there or you could do instead:
filesaveLocation = filesaveLocation.Replace("\\\\", "\\");
just before using Directory.Exists.
Jignesh TrivediRank #16 · 50.3K reputationPosted Jan 28, 2013, 5:45 PM EST
I think there is no problem in you code, it might directory exists in the passed location.
i hvae try following code it work fine for me in VS2010 / windows 7 platform.
string filesaveLocation = "E:\\\\Trans\\12-2012\\";
bool bo = Directory.Exists(filesaveLocation);
it return true when directory is exist else return false.
hope this will help you.
dcRank #2,247 · 627 reputationPosted Jan 29, 2013, 3:20 AM EST
Can you tell me why this would make a dufference?
VulpesRank #55 · 29.2K reputationPosted Jan 29, 2013, 4:29 AM EST
I assume therefore that you're not using Windows 7.
dcRank #2,247 · 627 reputationPosted Jan 29, 2013, 4:42 AM EST