.net路径访问被拒绝,.net在对文件夹和文件进行加密和压缩时提示路径访问被拒绝错误
对路径“E:\****\WebRoot\Exam\ExamTm\222”的访问被拒绝。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.UnauthorizedAccessException: 对路径“E:\****\WebRoot\Exam\ExamTm\222”的访问被拒绝。 ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。源错误: 
行 33:         public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
行 34:         {
行 35:             FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
行 36: 
行 37:             FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
 源文件: E:\****\Utility\FileEncryptDecrypt.cs    行: 35 堆栈跟踪: 
[UnauthorizedAccessException: 对路径“E:\****\WebRoot\Exam\ExamTm\222”的访问被拒绝。]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +664
   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) +1295
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) +53
   StudyV.Utility.FileEncryptDecrypt.EncryptFile(String sInputFilename, String sOutputFilename, String sKey) in E:\****\Utility\FileEncryptDecrypt.cs:35
   Admin_TestPlan_PaperDownLoad.btn_Delive_Click(Object sender, EventArgs e) in e:\****\WebRoot\Admin\TestPlan\PaperDownLoad.aspx.cs:62
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +104
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5594 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.1433; ASP.NET 版本:2.0.50727.1879 

解决方案 »

  1.   

    可以是IIS配置问题,也可能是FORM验证模式,而你没有登陆,产生票根所致。
      

  2.   

    iis上网站发布时赋足够的权限
      

  3.   

    sInputFilename
    打印出来看看是文件夹路径还是文件路径?
    这里必须是文件路径才可以

    E:\****\WebRoot\Exam\ExamTm\222\xxx.jpg
    而不能是文件夹路径
    E:\****\WebRoot\Exam\ExamTm\222
      

  4.   

    网站文件夹我赋予了最高权限,也不是form验证问题,这些我都试过了
      

  5.   

    建立站点虚拟目录时候选择允许读写,要不然你就直接读你站点下的目录,IIS安全限制是默认不允许你读写磁盘
      

  6.   

    sInputFilename
    打印出来看看
      

  7.   

    代码帖出来,大家看一下,具体是什么问题?先谢谢大家 
    /// <summary>
        /// 下载试卷信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Delive_Click(object sender, EventArgs e)
        {
            long tk_cl_Id = new StudyV.Model.IdentifierFactory().GetTK_Cl_ID(Convert.ToInt64(Request.QueryString["TestPlanId"]));
            string time=DateTime.Now.ToString("yyyyMMddhhmmss");
            #region 加密文件并压缩
            string sSecretKey;        // Get the Key for the file to Encrypt.
            sSecretKey = FileEncryptDecrypt.GenerateKey();// GenerateKey();        // For additional security Pin the key.
            GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
            DirectoryInfo dir = new DirectoryInfo(ConfigurationManager.AppSettings["Dir"] + tk_cl_Id);
      
            FileInfo[] file = dir.GetFiles();
            //创建临时文件夹
            Directory.CreateDirectory(ConfigurationManager.AppSettings["Dir"] + time + "\\" + tk_cl_Id);
            //对考试试卷逐一进行加密
            for (int i = 0; i < file.Length; i++)
            {
                //开始加密
                FileEncryptDecrypt.EncryptFile(ConfigurationManager.AppSettings["Dir"] + tk_cl_Id, ConfigurationManager.AppSettings["Dir"] + time + "\\" + tk_cl_Id, sSecretKey);
            }
            //开始压缩
            FileCompression.CompressDirectory(ConfigurationManager.AppSettings["Dir"] + time + "\\" + tk_cl_Id, ConfigurationManager.AppSettings["Dir"] + time+"\\试卷文件.zip", 6, false);
            Response.Redirect(ConfigurationManager.AppSettings["Dir"]+ time + "\\试卷文件.zip");
            FileCompression.DeleteFolder(ConfigurationManager.AppSettings["Dir"] + time);
            #endregion
        }
      

  8.   

    我也是同样的问题,希望高手不吝赐教
    {"对路径“\\\\Jutalserver\\engineering\\4.暂存\\设计中心文档库\\J801\\电仪”的访问被拒绝。"}
      

  9.   

    添加 Network Service这个帐号的写入权限