We help IT Professionals succeed at work.

Verify Existence of Directory

JimiJ13
JimiJ13 asked
on
Dear Experts,

I have a  folder  in Table column  Path = "D:\DL\NewRpt\" that physically exists.

However, when I check using  Directory.Exists("D:\DL\NewRpt\")  always return negative.


Any help would be gladly appreciated.
Comment
Watch Question

JimiJ13I T Consultant

Author

Commented:
It will return true if I use Directory.Exists(@"D:\DL\NewRpt\")  but how it can be formatted that way upon retrieving the value from table column?
Most Valuable Expert 2011
Top Expert 2015

Commented:
Because you're forgetting that backslash means something special inside of C# strings. You need to escape the slashes in order to treat them as a part of the path and not special characters. You can do that in two ways:

Double-Up
Directory.Exists("D:\\DL\\NewRpt\\")

Open in new window


Use the @ Modifier
Directory.Exists(@"D:\DL\NewRpt\")

Open in new window

JimiJ13I T Consultant

Author

Commented:
That's right but how it can be formatted using column value as variable?
Most Valuable Expert 2011
Top Expert 2015

Commented:
I do not understand the question.
JimiJ13I T Consultant

Author

Commented:
The Directory can be dynamically at the table Settings with the value as shown attached. That value shall be retrieved every time, as is, but it will not be recognized by C# unless formatted as:   "D:\\DL\\NewRpt\\" or @"D:\DL\NewRpt\".

 I hope that makes sense.
Directory.png
Most Valuable Expert 2011
Top Expert 2015
Commented:
Since you are receiving that value from the user, there is no formatting required--C# will take care of it. The user can type it in the same way that is shown in your screenshot. If you're getting an error from what is shown in the screenshot, then the directory does not exist.
JimiJ13I T Consultant

Author

Commented:
I wonder why I was not getting that same result earlier, now is working fine. Thanks anyway for leveling up my understanding.
JimiJ13I T Consultant

Author

Commented:
Thanks.