Unauthorized Access exception when i try to create a directory

北慕城南 提交于 2019-12-24 20:24:11

问题


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?


回答1:


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




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/16833482/unauthorized-access-exception-when-i-try-to-create-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!