Lamloumi2
Lamloumi2

Reputation: 235

Unauthorized Access exception when i try to create a directory

i have an exception Unauthorized Access when i did this :

if (fileurl != null && fileurl.ContentLength > 0)
                {
                 var fileName = Path.GetFileName(fileurl.FileName);
                // var path = AppDomain.CurrentDomain.BaseDirectory + fileName;
                 var path = Path.Combine(@"C:\Projets", fileName);
                 fileurl.SaveAs(path);
                 string path2 = Path.GetFileNameWithoutExtension(path);
                 Directory.CreateDirectory(path2);

I saved a compressed file fileurl.SaveAs(path); and i'd like o create a new directory in the same path. i verify the path path2 and it is ok. but the instruction Directory.CreateDirectory(path2); failed and the exception of Unauthorized Access appears .

Why this happens ? how can i fix it?

Upvotes: 0

Views: 3451

Answers (4)

Eric Leroy
Eric Leroy

Reputation: 1836

It sounds like a permissions problem. Make sure that your user has read/write file permissions.

Upvotes: 2

ZZZ
ZZZ

Reputation: 2812

You need to check the identity of the app pool running your web app. The default identify has not permission to C:\Projets and alike. You need to explicitly grand the user or the user group of the identity with permission of writing to C:\Projets.

Upvotes: 0

Damith
Damith

Reputation: 63065

You tag this as ASP.NET so you can't do as console or windows form applications.

your web application run on server side and with different user account.

You need to give permission to app pool running user and also since this is web application you need to write to directory under your web site or another virtual directory which accessible by the app pool user.

Upvotes: 0

rh072005
rh072005

Reputation: 740

Did you mean to set the path to C:\Projets or C:\Projects ? You may get unauthorized access on a path that doesn't exist

Upvotes: 0

Related Questions