Problem Creating Directory

J

James Coleman

Hello,

The following error is appearing when attempting to create a directory
using the availale system.io methods:

System.IO.DirectoryNotFoundException: Could not find a part of the
path "D:\". at System.IO.__Error.WinIOError(Int32 errorCode, String
str) at System.IO.Directory.InternalCreateDirectory(String fullPath,
String path) at System.IO.Directory.CreateDirectory(String path) at
TestVBProject.createdirectorytest.Page_Load(Object sender, EventArgs
e)

The code works fine in my test environment (wide open permissions) but
fails on the production server at the hosting facility. We have tried
giving full permissions to the aspnet account and it still fails.
Windows 2003 is used on the production server.

Because there are shared users on the server, I am afraid to try
changing the user in machine.config to SYSTEM, but if that should be
attempted, let me know.

Any help on the matter is appreciated.

Thanks.
 
M

Michael O'Donovan [MSFT]

Hi,

If you are using Windows 2003, then its not the ASPNet account who is
actually executing the thread that is trying to create the folder. Its the
user account that is running the application pool bound to the IIS
application. This has changed from Windows 2000. Look in the properties of
the application pool, identity tab.

Also, if you want to confirm this, use the following in one of your .aspx
pages to see who its running as.

//sample
Response.Write("Current executing thread is " +
System.Security.Principal.WindowsIdentity.GetCurrent().Name);

Michael
 
J

James Coleman

Thanks for the reply. I had already checked out the account and found
that it wasn't the aspnet account, but giving the correct account full
permissions to the directory didn't solve the problem.

The solution to the problem was to give the calling account
permissions to the root directory, not just the root of the website.
The hosting facility, however, is not willing to grant me these
permissions, so I'm in a pickle.

I've tried running batch files and or calling the command line via
system.diagnostics.process, but it doesn't work either.

The hosting facility claimed it was a bug with Win2003. I don't know.
All I want to do is create a directory! I'm not that experience with
com, but wonder if I need to create an unmanaged com object to do it
and then call that in .net.

How can I get around needing access to the root? Apparently one
company was able to solve it:
http://support.mathsoft.com/mas/article.asp?id=893

I don't know how they did it however.

Thanks,

James
 
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
 

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