Access to the path is denied

一世执手 提交于 2019-11-26 01:58:09

问题


I know this question was asked many times here, but I can\'t find a solution to my problem. I\'m trying to save image to the folder in .net c# but get this exception:

Access to the path \'C:\\inetpub\\wwwroot\\mysite\\images\\savehere\' is denied.The error occured at mscorlib because    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)

I gave full control to this folder (savehere) to network service and iis_iusrs, even gave full control to everyone but still getting this exception. I tried to give access via explorer and via IIS manager, still no luck

I\'m doing it on Windows server 2008 R2 and IIS 7.5, Who do I need to give access?

Thanks


回答1:


You need to find out from the application pool for the website what is the identity it is running under (by default this is Application Pool Identity) and grant that the correct permissions.




回答2:


Access to the path 'C:\inetpub\wwwroot\mysite\images\savehere' is denied

Read the message carefully. You are trying to save to a file that has the same name as the directory. That cannot work, you can't overwrite a directory filled with files with a single new file. That would cause undiagnosable data loss, "Access to the path is denied" is the file system fighting back to prevent that from happening.

The exception message is not ideal, but it comes straight from the OS and they are cast in stone. The framework often adds extra checks to generate better messages, but this is an expensive test on a network. Perf is a feature too.

You need to use a name like 'C:\inetpub\wwwroot\mysite\images\savehere\mumble.jpg'. Consider Path.Combine() to reliably generate the path name.




回答3:


I was having the same problem while trying to create a file on the server (actually a file that is a copy from a template).

Here's the complete error message:

{ERROR} 08/07/2012 22:15:58 - System.UnauthorizedAccessException: Access to the path 'C:\inetpub\wwwroot\SAvE\Templates\Cover.pdf' is denied.

I added a new folder called Templates inside the IIS app folder. One very important thing in my case is that I needed to give the Write (Gravar) permission for the IUSR user on that folder. You may also need to give Network Service and ASP.NET v$.# the same Write permission.

After doing this everything works as expected.




回答4:


I had exactly the same problem.

The solution was that the file I was trying to access was readonly, as it was copied from a template file that was readonly.

<facepalm />




回答5:


I got this problem when I try to save the file without set the file name.

Old Code

File.WriteAllBytes(@"E:\Folder", Convert.FromBase64String(Base64String));

Working Code

File.WriteAllBytes(@"E:\Folder\"+ fileName, Convert.FromBase64String(Base64String));



回答6:


My problem was that I had to ask for Read access only:

FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read);



回答7:


please add IIS_IUSERS full control permission to your folder. you find this option from security tab in folder properties.




回答8:


What Identity is your Application Pool for the Web application running as, to troubleshoot, try creating a new App Pool with say Network Service as its identity and make your web application use that new App Pool you created and see if the error persists.




回答9:


The following tip isn't an answer to this thread's original question, but might help some other users who end up on this webpage, after making the same stupid mistake I just did...

I was attempting to get an ASP.Net FileUpload control to upload it's file to a network address which contained a "hidden share", namely:

\MyNetworkServer\c$\SomeDirectoryOrOther

I didn't understand it. If I ran the webpage in Debug mode in Visual Studio, it'd work fine. But when the project was deployed, and was running via an Application Pool user, it refused to find this network directory.

I had checked which user my IIS site was running under, gave this user full permissions to this directory on the "MyNetworkServer" server, etc etc, but nothing worked.

The reason (of course!) is that only Administrators are able to "see" these hidden drive shares.

My solution was simply to create a "normal" share to

\MyNetworkServer\SomeDirectoryOrOther

and this got rid of the "Access to the path... is denied" error. The FileUpload was able to successfully run the command

fileUpload.SaveAs(networkFilename);

Hope this helps some other users who make the same mistake I did !

Note also that if you're uploading large files (over 4Mb), then IIS7 requires that you modify the web.config file in two places. Click on this link to read what you need to do: Uploading large files in ASP.Net




回答10:


I Solved with this setting:

IIS > Application Pools > [your site] > Advanced Settings... > Identity > Built-in accound > LocalSystem




回答11:


My problem was something like that:

FileStream ms = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);

but instead of using path I should use File.FullName... I don't know if it's going to help anyone else, just passing my own experience with this erro given!




回答12:


  1. Change the setting from built-in account to custom account and enter the other server's username and password.

  2. Keep the setting as integrated (instead of classic mode).




回答13:


Maybe it'il help you.

string tempDirectoryPath = @"C:\Users\HOPE\Desktop\Test Folder";
string zipFilePath = @"C:\Users\HOPE\Desktop\7za920.zip";
Directory.CreateDirectory(tempDirectoryPath);
ZipFile.ExtractToDirectory(zipFilePath, tempDirectoryPath);



回答14:


Make Directory savehere to be virtual directory and give read/write permission from control panel




回答15:


Had a directory by the same name as the file i was trying to write, so people can look out for that as well.




回答16:


I encountered this problem while developing on my local workstation.

After several unsuccessful iisreset invocations, I remedied this situation by rebooting my machine.

In retrospect, an open file handle may have been causing issues.




回答17:


In my case I had to add a .NET Authorization Rule for the web site in IIS.

I added a rule to allow anonymous users.




回答18:


I had the same problem but I fixed it by saving the file in a different location and then copying the file and pasting it in the location where I wanted it to be. I used the option to replace the the existing file and that did the trick for me. I know this is not the most efficient way but it works and takes less than 15 seconds.




回答19:


I had a lot of trouble with this, specifically related to my code running locally but when I needed to run it on IIS it was throwing this error. I found that adding a check to my code and letting the application create the folder on the first run fixed the issue without having to mess with the folders authorizations.

something like this before you call your method that uses the folder

bool exists = System.IO.Directory.Exists("mypath");

        if (!exists)
            System.IO.Directory.CreateDirectory("mypath");



回答20:


If you get this error while uploading files in Sub domain And working correct in your localhost, then follow below steps:

Solution:

Plesk Panel

  • Login to your Plesk Panel. Select Your Sub domain which is giving error.
  • Click on Hosting settings.
  • Select Additional write/modify permissions and Apply.

CPanel

  • I am not sure about options available in CPanel. But IF you give permission to directory (In CPanel it has to be decimal number like 777, 755) will resolve the error.

For more details refer here

Reason for Error:

  • Let's Assume FileUpload.SaveAs(Server.MapPath("~/uploads/" + *YOUR_FILENAME*)) will be your code to move your files to upload path.
  • Server.MapPath will give you physical path (Real Path) of directory. But your Sub domain may don't have permission for access physical path.

  • So, If you give permission for sub domain to access write/modify permission, it will resolve the issue.




回答21:


I created a virtual dir with full permission and added the ffmpeg source and video files there, so finally it made sense as it can be acess by anyone.



来源:https://stackoverflow.com/questions/4877741/access-to-the-path-is-denied

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