473,770 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Framework Bug: CreateDirectory throws DirectoryNotFou ndException

Hello,

I'm having a problem when trying to use the Directory.Creat eDirectory()
method. It throws a DirectoryNotFou ndException 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.Creat eDirectory( "d:\Sites\MySit e\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 DirectoryNotFou ndException 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 4608
Hmmm, I just took all read permissions off of a directory I named Test.
Then I used
Directory.Creat eDirectory("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.Creat eDirectory( "d:\Sites\MySit e\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
DirectoryNotFou nd

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******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello,

I'm having a problem when trying to use the Directory.Creat eDirectory()
method. It throws a DirectoryNotFou ndException 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.Creat eDirectory( "d:\Sites\MySit e\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 DirectoryNotFou ndException 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..unauthori zed
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 directoryNotFou nd 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******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello,

I'm having a problem when trying to use the Directory.Creat eDirectory()
method. It throws a DirectoryNotFou ndException 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.Creat eDirectory( "d:\Sites\MySit e\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 DirectoryNotFou ndException 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.Direc tory.CreateDire ctory( @"d:\Sites\FCKe ditor\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:
[DirectoryNotFou ndException: 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********@com cast.nospam.net > wrote in message
news:#c******** ******@TK2MSFTN GP09.phx.gbl...
Hmmm, I just took all read permissions off of a directory I named Test.
Then I used
Directory.Creat eDirectory("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.Creat eDirectory( "d:\Sites\MySit e\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
DirectoryNotFou nd

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******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello,

I'm having a problem when trying to use the Directory.Creat eDirectory()
method. It throws a DirectoryNotFou ndException 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.Creat eDirectory( "d:\Sites\MySit e\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 DirectoryNotFou ndException 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.Creat eDirectory(path ) and DirectoryInfo.C reateSubdirecto ry(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.Creat eDirectory() with call to unmanaged code, like msvcrt's _mkdir(char*):


[DllImport("msvc rt.dll", SetLastError=tr ue)]

static extern int _mkdir(string path);


....

//replace call to Directory.Creat eDirectory with:

_mkdir(newDirec tory);

....



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******** ******@tk2msftn gp13.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.Direc tory.CreateDire ctory( @"d:\Sites\FCKe ditor\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:
[DirectoryNotFou ndException: 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********@com cast.nospam.net > wrote in message
news:#c******** ******@TK2MSFTN GP09.phx.gbl...
Hmmm, I just took all read permissions off of a directory I named Test.
Then I used
Directory.Creat eDirectory("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.Creat eDirectory( "d:\Sites\MySit e\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
DirectoryNotFou nd

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******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello,

I'm having a problem when trying to use the Directory.Creat eDirectory()
method. It throws a DirectoryNotFou ndException 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.Creat eDirectory( "d:\Sites\MySit e\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 DirectoryNotFou ndException 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
5272
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 part of the path "D:\". My site is hosted on a public ISP that for obvious security reasons does not allow my read access above my wwwroot folder which seems to be a problem when trying to create directories... Is there any way to solve this?
2
4130
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: "C:\JumpStart\JumpStart\sites\Proto\images\sites.e-Plaza.org\Alece" Everything before 'Alece' already exists.
9
13753
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: Could not find a part of the path "D:\". The full path is "D:\hshome\clinton\test.gotchasoft.com\verify" The web is running using impersonation as a user on the local machine.
5
574
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 following (existing) directory: d:\Sites\MySite\ And you whant to create the following:
0
2199
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: System.IO.DirectoryNotFoundException - Could not find a part of the path "\\servername\foldername$" Do I need to have an absolute path and not a relative path, which the dollar
2
2515
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 value of folderPath is
6
1084
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 Security.SecurityException (which I found in the Object Browser with VS2005). If Path is invalid, the block catches System.IO.DirectoryNotFoundException as expected. However, if NetworkPath is invalid, it instead throws a System.IO.IOException,...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.