我试图保存一个图像到。net c#的文件夹,但我得到这个异常:

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)

我完全控制了这个文件夹(savehere)到网络服务和iis_iusrs,甚至完全控制了每个人,但仍然得到这个异常。 我尝试通过浏览器和IIS管理器访问,仍然没有运气

我是在Windows server 2008 R2和IIS 7.5上做的,我需要给谁访问权限?


当前回答

您的Web应用程序运行的应用程序池的身份是什么,为了排除故障,尝试创建一个新的应用程序池,例如网络服务作为其身份,并让您的Web应用程序使用您创建的新应用程序池,看看错误是否仍然存在。

其他回答

我遇到了很多麻烦,特别是与我的代码在本地运行有关,但当我需要在IIS上运行它时,它抛出了这个错误。我发现,在我的代码中添加一个检查,并让应用程序在第一次运行时创建文件夹,就可以解决这个问题,而不必打乱文件夹的授权。

在调用使用文件夹的方法之前

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

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

您需要从网站的应用程序池中找出它运行的身份(默认情况下是应用程序池身份),并授予正确的权限。

您的Web应用程序运行的应用程序池的身份是什么,为了排除故障,尝试创建一个新的应用程序池,例如网络服务作为其身份,并让您的Web应用程序使用您创建的新应用程序池,看看错误是否仍然存在。

我的问题是,我必须要求只读访问:

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

我解决了这个设置:

IIS >应用程序池>[您的站点]>高级设置…> 标识>内置帐户> LocalSystem