Error access error

I am trying to delete excel file from specipic location. but cannot delete. with an error:

Access to path "C: \ mypath \ sample.xlsx" is denied.

I am writing code like:

protected void imgbtnImport_Click(object sender, ImageClickEventArgs e)
{

    try
    {
        string strApplicationPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
        string strXLStoredDirectoryPath = strApplicationPath + "/Information Documents/";
        DirectoryInfo di = new DirectoryInfo(strXLStoredDirectoryPath);
        string fileName = flUpldSelectFile.FileName;
        if (!File.Exists(strXLStoredDirectoryPath))
        {
            Directory.CreateDirectory(strXLStoredDirectoryPath);

            di.Attributes = FileAttributes.Normal;
        }
        string strCreateXLFileDestinationPath = strXLStoredDirectoryPath + fileName;
        if (File.Exists(strCreateXLFileDestinationPath))
        {
            File.Delete(strCreateXLFileDestinationPath);
        }

        flUpldSelectFile.SaveAs(strCreateXLFileDestinationPath);           
        di.Attributes = FileAttributes.ReadOnly;
    }
    catch (Exception)
    {            
        throw;
    }
}

please direct .........

- ************************************************* *********************** Another problem. he is not allowed. getting a UnauthorizedAccessException. since access is denied to delete the file. I'm tired. please help; I've tried a lot. - *************************************************** *********************** Could there be a flaw in VSS? i use this

+3
source share
10 answers

Try a combination of these two steps:

  • IIS , ( , ). IIS7.
  • web.config <system.web>:
    <identity impersonate="true"/>

    <identity impersonate="true" userName="contoso\Jane" password="password"/>

+3

:

, / . "" " ", - . FileStream Using, , , .

if flUpldSelectFile.SaveAs(strCreateXLFileDestinationPath); , , SaveAs. , FileStream Using.

, flUpldSelectFile, , System.Web.UI.WebControls.FileUpload. .

using (FileStream fs = new FileStream(strCreateXLFileDestinationPath, FileMode.Create))
{        
    byte[] buffer = flUpldSelectFile.FileBytes;
    fs.Write(buffer, 0, buffer.Length);
}

, , , .

, , / .

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx alt text
(: microsoft.com)

+11

IIS, , ASPNET , , , , ASPNET

.

+4

, , . 2 $, .

+2

, , IIS . , .

, ASPNET . . , , IIS, . Windows XP ASPNET. Windows Server 2003, 2008 Windows Vista 7 NETWORK_SERVICE.

. question.

+2

,   /.

+1

, ASPNET file\( file\folder Windows . , ASPNET).

+1

. , , IIS, .

, ProcMon: Proc Mon . ProcMon, , . . Ctrl-E, , Ctrl-F, . , . , ( ), , . "", , .

, , , , , , , , // .

-, , , , . , , .

+1

, ?

0

I don’t think we have enough information to be useful. What is a security context (identification) during a Delete call? Is the application personifying the end user? If so, how are they authenticated? If in Windows / Active Directory you need to check user permissions for a specific file. If by Forms login you probably should not impersonate and verify that the AppPool security context has the appropriate permissions.

0
source

Source: https://habr.com/ru/post/1734068/


All Articles