我试图保存一个图像到。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上做的,我需要给谁访问权限?


当前回答

拒绝访问路径“C:\inetpub\wwwroot\mysite\images\savehere”

仔细阅读这条信息。您正在尝试保存到与该目录同名的文件。这是行不通的,您不能用一个新文件覆盖一个充满文件的目录。这将导致无法诊断的数据丢失,“对路径的访问被拒绝”是文件系统的反击,以防止这种情况发生。

异常消息并不理想,但它直接来自操作系统,而且它们是固定不变的。该框架通常会添加额外的检查以生成更好的消息,但这在网络上是一项昂贵的测试。性能也是一个特性。

你需要使用一个像“C:\inetpub\wwwroot\mysite\images\savehere\mumble.jpg”这样的名字。考虑path . combine()来可靠地生成路径名。

其他回答

有一个与我试图写的文件同名的目录,所以人们也可以找到它。

也许对你有帮助。

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

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

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

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

我的问题大概是这样的:

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

但不是使用path,我应该使用File.FullName… 我不知道它是否会帮助其他人,只是传递我自己的经验与这个错误!