Framework Bug: CreateDirectory throws DirectoryNotFoundException

  • Thread starter Frederico Caldeira Knabben
  • Start date
F

Frederico Caldeira Knabben

Hello,

I'm having a problem when trying to use the Directory.CreateDirectory()
method. It throws a DirectoryNotFoundException exception. I'll try to
illustrate it:

Suppose you have the following (existing) directory:
d:\Sites\MySite\

And you whant to create the following:
d:\Sites\MySite\MyDir1\MyDir2\

It should be an easy task. Just call CreateDirectory(), like this:
Directory.CreateDirectory( "d:\Sites\MySite\MyDir1\MyDir2\" ) ;

And it will create all missing dirs... in this case "MyDir1" and "MyDir2".

But... if you don't have read permissions of the "d:\" or "d:\Sites\"
directory, a DirectoryNotFoundException exception is thrown.

I took a look around and many people are facing the same problem. My opinion
is that it is a "big bug" in the DotNet Framework implementation.

The problem is the way the method works... it check if exists and create the
directories starting from the root in this order:
d:\ (Check exists)
d:\Sites\ (Check exists)
d:\Sites\MySite\ (Check exists)
d:\Sites\MySite\MyDir1\ (Check exists and create)
d:\Sites\MySite\MyDir1\MyDir2\ (Check exists and create)

It should do the inverse... starting from the back until the directory is
found (adding the missing ones in and array and after that create then).

It is easy to solve the problem in a controlled environment. You just need
to give Read permissions to the entire path starting from the root (even if
you don't want to do that). But in other cases (like a site hosted in a
server were you have no control), this is impossible to do.

Is there a workaround to create a directory in these situations?

Thanks in advance,
Frederico Caldeira Knabben
 
W

William Ryan eMVP

Hmmm, I just took all read permissions off of a directory I named Test.
Then I used
Directory.CreateDirectory("C:\Test\Test\Test\Test")

and had no problem. Mind you that C:\Test had no read permissoins, and one
by one I used CreateDirectory. Then I deleted everything past the root node
and execute the line above. Either way, no problem.

Is this the exact line that gives you the problems?
Directory.CreateDirectory( "d:\Sites\MySite\MyDir1\MyDir2\" ) ;? If so
there's an error there, consistent with what the documentation says will
happen in such cases
http://msdn.microsoft.com/library/d...temiodirectorynotfoundexceptionclasstopic.asp
You either need to put a @ at the beginning or use "\\" where you have
single ones. I've tried it a few different ways and can't get it to happen.
But if I put some illegal characters in there, no problemo, kaboom with a
DirectoryNotFound

Unless you left something out to replicate this problem, I'm not sure it's a
bug.
--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
 
W

William Ryan eMVP

I've tried it singleing out each permission. I'm not seeing any
inconsistent behavior. I'm running 2000 Pro .

Also, I did it in C# and using the exact code you posted, I can't even get
it to compile so I'm guessing that's not the actual code..unauthorized
escape sequence. I've butchered it every way I know how. Now, if I use D:\
which is CD Rom on my machine, I get directoryNotFound even though D:\
Exists. According to the documentation, this is consistent.

Cheers,

Bill

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
 
F

Frederico Caldeira Knabben

Hello William,

Thanks for the prompt response.

Sorry for the slashes... that was just a sample I wrote in the e-mail to
make you understand the problem.

Ok.. I just made a new sample... with a ASPX page... inside the onload
method of the page I put:

System.IO.Directory.CreateDirectory( @"d:\Sites\FCKeditor\Test\" ) ;

I have just Administrator permissions over the "d:\" and full permissions to
the "IIS_WPG" user starting from "d:\Sites\"... the following error occours:
[DirectoryNotFoundException: Could not find a part of the path "D:\".]

If I go to the "d:\" directory and give just "Read" permissions to the
"IIS_WPG" user, everything works just fine.

I'm using Window 2003 with Framework 1.1.

I hope you or someone else can help me.

Best regards,
FredCK
 
D

Dmitry Kulakovsky

This is a relatively known .NET bug or "feature"...




Both Directory.CreateDirectory(path) and DirectoryInfo.CreateSubdirectory(path) require user to have Read access to the drive's root directory (i.e. <Drive>:\).




Many ASP.NET hosting providers (especially those running Windows 2003 Server) will not allow user running ASP.NET worker process read access to the root folder, so CreateDirectory will always fail. You can not blame hosting providers - they do right thing, securing shared environment from users with malicious intents.




The only workaround I have found is to replace call to Directory.CreateDirectory() with call to unmanaged code, like msvcrt's _mkdir(char*):




[DllImport("msvcrt.dll", SetLastError=true)]

static extern int _mkdir(string path);




....

//replace call to Directory.CreateDirectory with:

_mkdir(newDirectory);

....



This will work only if your code is granted "Allow Calls to Unmanaged Code" permission but most hosting environments allow that.



You can find more details in my recent Blog entry at http://hatka.net/wlogdev/archive/2004/08/29/178.aspx



Dmitry Kulakovsky



Frederico Caldeira Knabben said:
Hello William,

Thanks for the prompt response.

Sorry for the slashes... that was just a sample I wrote in the e-mail to
make you understand the problem.

Ok.. I just made a new sample... with a ASPX page... inside the onload
method of the page I put:

System.IO.Directory.CreateDirectory( @"d:\Sites\FCKeditor\Test\" ) ;

I have just Administrator permissions over the "d:\" and full permissions to
the "IIS_WPG" user starting from "d:\Sites\"... the following error occours:
[DirectoryNotFoundException: Could not find a part of the path "D:\".]

If I go to the "d:\" directory and give just "Read" permissions to the
"IIS_WPG" user, everything works just fine.

I'm using Window 2003 with Framework 1.1.

I hope you or someone else can help me.

Best regards,
FredCK






William Ryan eMVP said:
Hmmm, I just took all read permissions off of a directory I named Test.
Then I used
Directory.CreateDirectory("C:\Test\Test\Test\Test")

and had no problem. Mind you that C:\Test had no read permissoins, and one
by one I used CreateDirectory. Then I deleted everything past the root node
and execute the line above. Either way, no problem.

Is this the exact line that gives you the problems?
Directory.CreateDirectory( "d:\Sites\MySite\MyDir1\MyDir2\" ) ;? If so
there's an error there, consistent with what the documentation says will
happen in such cases
http://msdn.microsoft.com/library/d...temiodirectorynotfoundexceptionclasstopic.asp
You either need to put a @ at the beginning or use "\\" where you have
single ones. I've tried it a few different ways and can't get it to happen.
But if I put some illegal characters in there, no problemo, kaboom with a
DirectoryNotFound

Unless you left something out to replicate this problem, I'm not sure it's a
bug.
--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top