Forum Moderators: open

Message Too Old, No Replies

Directory.Exists problem

         

IntegrityWebDev

8:19 pm on Aug 6, 2010 (gmt 0)

10+ Year Member



I am having a problem with a piece of code for a web app and hoping you guys can help. What I am needing to do is see if a certain directory exists based on what has been entered into a text box (www.example.com/textinbox).

I have this code:

string directoryString = Server.MapPath(".") + @"\" + directoryname.Text;
if (Directory.Exists (directoryString))
{
Do this stuff.
}
else
{
Do other stuff.
}


However for some reason the first one ALWAYS passes, even if the full path does not exist.

Two questions:
1) Why is this happening?
2) Is there a better way to test to see if a directory exists?

Thanks all!
Chris

Ocean10000

5:20 am on Aug 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The only way I can see this happening is if directoryname.Text = string.empty, then it will always return true.

I did a quick and dirty sample and couldn't duplicated it any other way.

Depending when you are checking directoryname.Text, it might not of been populated yet, and in other later events it was.

IntegrityWebDev

8:34 pm on Aug 7, 2010 (gmt 0)

10+ Year Member



I will check that monday but I know its inside of an IsPostBack that is working properly...thanks!

IntegrityWebDev

12:52 pm on Aug 9, 2010 (gmt 0)

10+ Year Member



Found it...before the test I had a piece of code that was actually creating the directory each time...so it WAS always true. :-) Thanks!