简体   繁体   English

访问被拒绝错误

[英]Access denied error

I am trying to delete the excel file from a specipic location . 我试图从specipic位置删除excel文件。 but can't deleting. 但不能删除。 having error : 有错误:

Access to the path 'C:\\mypath\\sample.xlsx' is denied. 访问路径'C:\\ mypath \\ sample.xlsx'被拒绝。

I write a code as : 我写了一个代码:

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 guide......... 请指导.........

-*********************************************************************** Still problem there . - ******************* **********************那里还有问题。 it is not resolved . 它没有解决。 getting UnauthorizedAccessException. 获取UnauthorizedAccessException。 as access denied to deleting file. 因访问被拒绝删除文件。 I m tired now . 我现在累了。 please help; 请帮忙; I tried many things..please help -*********************************************************************** Is may be iffect of VSS ? 我尝试了很多东西..请帮忙 - ************************************* ****************************** VSS可能是否有效? i am using that 我正在使用它

UPDATE: 更新:

Part of your issue might be what is saving/creating this file. 部分问题可能是保存/创建此文件。 If you're using a built in "Save" or "SaveAs" feature the underlying file stream might still have a lock on the file. 如果您使用内置的“保存”或“另存为”功能,则基础文件流可能仍会锁定文件。 writing your own save logic with a FileStream wrapped in a Using statement will help dispose the stream right when you're done thus allowing you to further manipulate the file within the same context. Using包含在Using语句中的FileStream编写自己的保存逻辑将有助于在完成后立即处理流,从而允许您在相同的上下文中进一步操作文件。

if flUpldSelectFile.SaveAs(strCreateXLFileDestinationPath); if flUpldSelectFile.SaveAs(strCreateXLFileDestinationPath); is the only logic that saves the file then get rid of the built in SaveAs functionality. 是保存文件然后摆脱内置的SaveAs功能的唯一逻辑。 write your own save logic using a FileStream wrapped in a Using block. 使用包含在Using块中的FileStream编写自己的保存逻辑。

In your example i can't see what flUpldSelectFile is so i am assuming it is a System.Web.UI.WebControls.FileUpload control. 在您的示例中,我无法看到flUpldSelectFile是什么,所以我假设它是一个System.Web.UI.WebControls.FileUpload控件。 Here is an example of rolling your own save logic. 以下是滚动自己的保存逻辑的示例。

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

As stated previously, use this tool to find out if there is a lock on the file by another process. 如前所述,使用此工具可以确定另一个进程是否存在对文件的锁定。

ORIGINAL 原版的

Pop open this wonderful tool and search for that file to see who/what has it locked 弹出这个奇妙的工具并搜索该文件,看看它/锁定了什么

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx 替代文字
(source: microsoft.com ) (来源: microsoft.com

If your code is working under IIS , Note that ASPNET user doesn't have access to computer files, you should give access to it, that is not recommended, or store you files in the place where ASPNET user have access 如果您的代码在IIS下工作,请注意ASPNET用户无权访问计算机文件,您应该访问它,不建议这样做,或者将文件存储在ASPNET用户可以访问的位置

see here 看到这里

Try a combination of these 2 steps: 尝试这两个步骤的组合:

  1. Set the IIS application pool to run under an account with privileges such as a domain account or local user account (not a default account like local service or local system). 将IIS应用程序池设置为在具有域帐户或本地用户帐户(不是默认帐户,如本地服务或本地系统)等权限的帐户下运行。 Instructions for IIS7 . IIS7的说明
  2. Turn impersonation on in the web.config file, in the <system.web> section: 在web.config文件的<system.web>部分中打开模拟:
    <identity impersonate="true"/>

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

I think the message is clear, you do not have authorization to delete the file or it is opened by another application. 我认为消息很明确,您没有权限删除该文件或由其他应用程序打开。 I bet 2$ you can't delete the file manually either. 我打赌2 $你也不能手动删除文件。

As others have said, this is because IIS runs your application as a user with restricted access rights. 正如其他人所说,这是因为IIS以具有受限访问权限的用户身份运行您的应用程序。 This is a wise security precaution, so that your system is less vulnerable to malicious attacks. 这是明智的安全预防措施,因此您的系统不易受到恶意攻击。

What you need to do is to give the ASPNET user access to the specific folder. 您需要做的是让ASPNET用户访问特定文件夹。 You do that from the security tab in the properties of a folder. 您可以从文件夹属性中的安全选项卡执行此操作。 The user you need to give full control to depends on the version of IIS you are using. 您需要完全控制的用户取决于您使用的IIS版本。 In Windows XP it is ASPNET. 在Windows XP中它是ASPNET。 In Windows Server 2003, 2008 and Windows Vista, 7 it is NETWORK_SERVICE. 在Windows Server 2003,2008和Windows Vista中,7是NETWORK_SERVICE。

See also this question for more details. 有关更多详细信息,请参阅此问题

确保该文件未被其他用户/进程打开或锁定。

确保ASPNET用户可以访问文件\\文件夹(使用Windows资源管理器检查文件\\文件夹的属性并转到安全选项卡。检查是否添加了ASPNET用户)。

One of two things are happening. 发生了两件事之一。 Either the file is already open, or the permission of the user running IIS does not have the proper permissions. 文件已经打开,或者运行IIS的用户的权限没有适当的权限。

Either way, this utility ProcMon: Proc Mon will help you determine the issue. 无论哪种方式,此实用程序ProcMon: Proc Mon将帮助您确定问题。 Run ProcMon, kick off your process to try and delete the file. 运行ProcMon,启动您的进程以尝试删除该文件。 Then go back to procmon. 然后回到procmon。 Hit Ctrl-E to turn off the capture, then Ctrl-F to find. 按Ctrl-E关闭捕获,然后按Ctrl-F查找。 Enter the name of the file you're trying to delete. 输入您要删除的文件的名称。 Then once you've found the correct line with the access denied (or similar error) Double click on the the line to get further information. 然后,一旦找到了拒绝访问的正确行(或类似错误),请双击该行以获取更多信息。 When you click on the Process tab, it will show you the exact user that is trying to delete the file. 单击“进程”选项卡时,它将显示尝试删除该文件的确切用户。

So, if it is a file permission issue, you now know the exact user, and can therefore go to the file system right click on the folder that houses the file you are trying to delete, and grant that user permissions to read/write/update that folder. 因此,如果它是文件权限问题,您现在知道确切的用户,因此可以转到文件系统,右键单击包含您要删除的文件的文件夹,并授予该用户读/写/的权限更新该文件夹。

Second, if the file is locked open instead of a permissions issue, you will have to find out what process is holding open the file. 其次,如果文件被锁定而不是权限问题,则必须找出保持打开文件的进程。 If you are also writing this file in another part of your code, perhaps you are not closing it properly or releasing the object reference. 如果您还在代码的另一部分中编写此文件,则可能未正确关闭它或释放对象引用。

您是否确认该文件没有设置只读属性?

I don't think we have enough info to be helpful. 我认为我们没有足够的信息来提供帮助。 What is the security context (identity) during the call to Delete? 在调用Delete期间,安全上下文(标识)是什么? Is the application impersonating the end user? 应用程序是否冒充最终用户? If it is, how are they authenticated? 如果是,他们如何进行身份验证? If by Windows / Active Directory, then you'll need to verify that user's access rights to the specific file. 如果是通过Windows / Active Directory,则需要验证该用户对特定文件的访问权限。 If by Forms login, then you should probably not impersonate and verify that the AppPool's security context has the appropriate access rights. 如果通过Forms登录,那么您可能不应该冒充并验证AppPool的安全上下文是否具有适当的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM