Why is access to the path denied?

2019-01-01 02:16发布

I am having a problem where I am trying to delete my file but I get an exception.

if (result == "Success")
{
     if (FileUpload.HasFile)
     {
         try
         {
              File.Delete(Request.PhysicalApplicationPath + app_settings.login_images + txtUploadStatus.Text);
              string filename = Path.GetFileName(btnFileUpload.FileName);
              btnFileUpload.SaveAs(Request.PhysicalApplicationPath + app_settings.login_images + filename);
         }
         catch (Exception ex)
         {
               Message(ex.ToString());
         }
      }
}

Also I should note that the folder I am trying to delete from has full control to network services.

The full exception message is:

System.UnauthorizedAccessException: Access to the path 'C:\Users\gowdyn\Documents\Visual Studio 2008\Projects\hybrid\hybrid\temp_loginimages\enviromental.jpg' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at hybrid.User_Controls.Imgloader_Add_Edit_Tbl.btnUpdate_Click(Object sender, EventArgs e) in C:\Users\gowdyn\Documents\Visual Studio 2008\Projects\hybrid\hybrid\User_Controls\Imgloader_Add_Edit_Tbl.ascx.cs:line 242

Any ideas?

23条回答
泛滥B
2楼-- · 2019-01-01 02:38

I too faced the same problem when trying to do this after deployment at server:

dirPath = Server.MapPath(".") + "\\website\\" + strUserName;
if (!Directory.Exists(dirPath))
{
    DirectoryInfo DI = Directory.CreateDirectory(dirPath);
}
string filePath = Server.MapPath(".") + "\\Website\\default.aspx";
File.Copy(filePath, dirPath + "\\default.aspx", true);
File.SetAttributes(dirPath + "\\default.aspx", FileAttributes.Normal);

I granted permission in IIS to other group including administrator and my problem got solved.

查看更多
高级女魔头
3楼-- · 2019-01-01 02:40

I got this error and solved it in just a moment. Don't know why all of my folders are read-only,I cancelled the read-only and apply it. However, it is still read-only. So I moved the file into the root folder, it works - so weird.

查看更多
高级女魔头
4楼-- · 2019-01-01 02:43

I have found that this error can occur in DESIGN MODE as opposed to ? execution mode... If you are doing something such as creating a class member which requires access to an .INI or .HTM file (configuration file, help file) you might want to NOT initialize the item in the declaration, but initialize it later in FORM_Load() etc... When you DO initialize... Use a guard IF statement:

    /// <summary>FORM: BasicApp - Load</summary>
    private void BasicApp_Load(object sender, EventArgs e)
    {
        // Setup Main Form Caption with App Name and Config Control Info
        if (!DesignMode)
        {
            m_Globals = new Globals();
            Text = TGG.GetApplicationConfigInfo();
        }
    }

This will keep the MSVS Designer from trying to create an INI or HTM file when you are in design mode.

查看更多
浪荡孟婆
5楼-- · 2019-01-01 02:45

I had the same problem on a newly moved website on a shared server. Solved through the web host panel (DotNetPanel) setting true the "allow write permissions". So if you are in a shared server before reviewing all code worth taking a look at the server configuration and could save you a lot of time.

查看更多
梦该遗忘
6楼-- · 2019-01-01 02:46

I also had the problem, hence me stumbling on this post. I added the following line of code before and after a Copy / Delete.

Delete

File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);

Copy

File.Copy(file, dest, true);
File.SetAttributes(dest, FileAttributes.Normal);
查看更多
深知你不懂我心
7楼-- · 2019-01-01 02:46

First just check the path if the colon(:) character is missing or not after the drive letter. If colon is not missing then you can check if access/write permission is granted for that path. I had the same issue and i was only missing the colon, permission and everything else was fine.

C:\folderpath

will work fine but,

C\folderpath .........(missing colon)

will give you access denial error.

查看更多
登录 后发表回答