473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Framework Bug: CreateDirectory throws DirectoryNotFoundException

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
Jul 21 '05 #1
4 4569
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/de...classtopic.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_...center_pg1.asp
"Frederico Caldeira Knabben" <fr****@fredck.com> wrote in message
news:uM****************@TK2MSFTNGP10.phx.gbl...
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

Jul 21 '05 #2
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_...center_pg1.asp
"Frederico Caldeira Knabben" <fr****@fredck.com> wrote in message
news:uM****************@TK2MSFTNGP10.phx.gbl...
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

Jul 21 '05 #3
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" <do********@comcast.nospam.net> wrote in message
news:#c**************@TK2MSFTNGP09.phx.gbl...
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/de...classtopic.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_...center_pg1.asp
"Frederico Caldeira Knabben" <fr****@fredck.com> wrote in message
news:uM****************@TK2MSFTNGP10.phx.gbl...
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


Jul 21 '05 #4
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" <fr****@fredck.com> wrote in message news:#j**************@tk2msftngp13.phx.gbl...
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" <do********@comcast.nospam.net> wrote in message
news:#c**************@TK2MSFTNGP09.phx.gbl...
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/de...classtopic.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_...center_pg1.asp
"Frederico Caldeira Knabben" <fr****@fredck.com> wrote in message
news:uM****************@TK2MSFTNGP10.phx.gbl...
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



Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Eran Kampf | last post by:
I am trying to dynamically create directories in my ASP.NET application (I am using Server.MapPath("/")+"test" as the folder) and I am getting a DirectoryNotFoundException saying "Could not find a...
2
by: GM | last post by:
Any help is greatly appreciated! I'm up against a meeting deadline. Directory.CreateDirectory Method throws UnauthorizedAccessException Here is the path: ...
9
by: Clinton Frankland | last post by:
Hi, On a Windows 2000 Server when attempting to use System.IO.Directory.CreateDirectory(string.concat(Server.MapPath(""), "\verify")) I receive a System.IO.DirectoryNotFoundException error:...
5
by: Frederico Caldeira Knabben | last post by:
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...
0
by: NewbieDev | last post by:
Hi, I call Directory.CreateDirectory and File.Copy from VB.NET with a UNC path that has a dollar sign ($) - "\\servername\foldername$\childfoldername\" and it gives me this error message: ...
2
by: =?Utf-8?B?Y2FzaGRlc2ttYWM=?= | last post by:
Hi, I am having a bit of a nightmare with the following: if(!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath); When I add a watch to this while debugging I see that the...
6
by: =?Utf-8?B?QnJpYW4gTmljaG9sc29u?= | last post by:
How do I know which exceptions a function throws? In my application, I make a call to DirectoryInfo.GetDirectories("\\NetworkPath\Path"). I check for System.IO.DirectoryNotFoundException and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.