Re: Create Directory

From: Dmitry Kulakovsky (dkulakovsky_at_comcast.net)
Date: 08/30/04


Date: Sun, 29 Aug 2004 23:24:50 -0400

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

"John" <nospam@please.com> wrote in message
news:opr6uojhcr22ma4c@cl213.castletown.pdms.com...
> Hello,
>
> I am having a permissions problem when creating a directory. The relevant
> bits of my code look like this:
>
> // impersonate current user:
> WindowsIdentity ident = (WindowsIdentity)
> HttpContext.Current.User.Identity;
> _context = ident.Impersonate();
> _name = ident.Name;
> _isauth = ident.IsAuthenticated;
> _authtype = ident.AuthenticationType;
>
> // create the directory
> // I've also tried using DirectoryInfo.Create()
> Directory.CreateDirectory(directoryName);
>
> // restore current (aspnet) identity:
> _context.Undo();
>
> If "directoryname" is "e:\inetpub\wwwroot\unittests\createdirectory\New" I
> am getting the error Could not find a part of the path "e:\".
>
> If I grant this user access to the root of my "e" drive then it works
> fine. However, doing this is a security risk (and also, to me, seems
> completely unnecessary).
>
> Does anyone know why this happens and if there is a sensible way round
> it? Is this a .net bug?
>
> Any help would be very much appreciated as I am completely stumped!
>
> Thanks,
>
> John



Relevant Pages

  • Re: Problem Creating Directory
    ... This is a relatively known .NET bug or "feature"... ... I had already checked out the account and found ... > permissions to the root directory, not just the root of the website. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Advice On DllImport
    ... I used MapPathto find the root directory of our webspace, ... "This is a relatively known .NET bug or "feature"... ... Many ASP.NET hosting providers (especially those running Windows 2003 ... The only workaround I have found is to replace call to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Framework Bug: CreateDirectory throws DirectoryNotFoundException
    ... This is a relatively known .NET bug or "feature"... ... static extern int _mkdir; ... This will work only if your code is granted "Allow Calls to Unmanaged Code" permission but most hosting environments allow that. ...
    (microsoft.public.dotnet.general)
  • Re: DirectoryNotFoundException - Could not find a part of the path "D:".
    ... the drive's root directory. ... hosting providers - they do right thing, ... Directory.CreateDirectorywith call to unmanaged code, ... > Parentfolder D:\ does not exist. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Create Directory
    ... I am having a permissions problem when creating a directory. ... However, doing this is a security risk (and also, to me, seems ... Does anyone know why this happens and if there is a sensible way round ... Is this a .net bug? ...
    (microsoft.public.dotnet.framework.aspnet.security)