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


当前回答

我也有同样的问题,但我通过将文件保存在不同的位置,然后复制文件并粘贴到我想要的位置来解决它。我使用这个选项来替换现有的文件,这对我来说很有用。我知道这不是最有效的方法,但它确实有效,而且用时不到15秒。

其他回答

我的问题大概是这样的:

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

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

您需要添加读写值的访问参数,如下所示

using (var stream = new FileStream(localPath, FileMode.Create, access : FileAccess.ReadWrite))
{
    file.CopyTo(stream);
} 

我也有同样的问题。

解决方案是我试图访问的文件是只读的,因为它是从只读的模板文件复制的。

< facepalm - >

将“内置帐户”修改为“自定义帐户”,并输入另一台服务器的用户名和密码。 保持设置为集成模式(而不是经典模式)。

确保你的目标在System.IO中。Delete(string file)是一个已经存在的文件。 也许你的代码中有错误,比如你没有将正确的文件名传递给方法,或者你的目标是一个文件夹。在这些情况下,你会看到:"access to the path is denied错误"。