简体   繁体   English

无法删除C#中的文件-未经授权的访问异常

[英]Can't delete a file in C# - unauthorized access exception

In my ASP.Net web application, I have loaded one image on the HTML 5 canvas and allow the user to draw some graphics (rectangle box) over the images. 在我的ASP.Net Web应用程序中,我已经在HTML 5画布上加载了一个图像,并允许用户在图像上绘制一些图形(矩形框)。 Once the user has finished their drawings on the image I have to save the image back to the server with the same name at same location. 用户在图像上完成绘图后,我必须将图像以相同的名称保存到服务器的相同位置。

I am using AJAX to transmit the image data to the server. 我正在使用AJAX将图像数据传输到服务器。 This part is done successfully. 这部分已成功完成。 In my server code, first I am trying to delete a file and then create a new file with the same name at same location. 在服务器代码中,首先,我尝试删除一个文件,然后在相同位置创建一个具有相同名称的新文件。

So, When I am deleting the file, it is raising UnAuthorizedAccessException is handled by user code Access to the path 'D:\\vs-2010projects\\delete_sample\\delete_sample\\myimages\\page_1.png' is denied. 因此,当我删除文件时,它正在引发UnAuthorizedAccessException is handled by user code Access to the path 'D:\\vs-2010projects\\delete_sample\\delete_sample\\myimages\\page_1.png' is denied.

Here is my Server Side C# Code... 这是我的服务器端C#代码...

[WebMethod()]
public static void UploadImage(string imageData)
{
    byte[] data = Convert.FromBase64String(imageData);
    if(File.Exists("D:\\vs-2010projects\\delete_sample\\delete_sample\\myimages\\page_1.png"))
    {
        File.Delete("D:\\vs-2010projects\\delete_sample\\delete_sample\\myimages\\page_1.png");
    }

    FileStream fs = new FileStream("D:\\vs-2010projects\\delete_sample\\delete_sample\\myimages\\page_1.png", FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);
    bw.Write(data);
    bw.Close();
}//UploadImage

Is there any way to delete a file? 有什么办法可以删除文件吗?

Please guide me out of this issue. 请引导我解决此问题。

First of all you should pack your stream statements into using clause that will automatically handle dispose action (even in case of exception) - it will save you a lot of time during debugging strange issues coming from not-closed streams 首先,您应该将流语句打包到using子句中,该子句将自动处理dispose操作(即使发生异常)-在调试来自未关闭流的奇怪问题时,它将为您节省大量时间

using(var fs = new FileStream(...)) 
{
  using(var bw = new BinaryWriter(fs)
  {
       bw.Write(data);
  }
}

Now the exception often comes because your current process does not have access rights for the file (can't delete file) - to solve it 现在,通常会出现异常,因为您当前的进程没有文件访问权限(无法删除文件)-解决该问题

  • add full permissions for your user 为您的用户添加完整权限

You can do this by finding the file in the Windows Explorer , checking its properties and under security tab you will find specific permissions. 您可以通过在Windows资源管理器中找到文件,检查其属性,然后在“安全性”选项卡下找到特定的权限来执行此操作。

For example if you host your page on IIS then it is identified as application pool identity which is either IIS_IUSRS or NETWORK SERVICE and those parties are usually not trusted (or not enough trusted to be able to delete file0 例如,如果您将页面托管在IIS上,则该页面将被标识为应用程序池标识,即IIS_IUSRS或NETWORK SERVICE,并且通常不信任这些参与方(或信任程度不足以删除文件0)

I presume, it has something to do with privilege. 我认为,这与特权有关。 When a user tries to connect to your Web site, IIS assigns the connection to the IUSER_ComputerName account, which belongs to the Guests group. 当用户尝试连接到您的网站时,IIS会将连接分配给IUSER_ComputerName帐户,该帐户属于来宾组。 This group has security restrictions. 该组具有安全限制。 Try to elevate access of IUSER_ComputerName. 尝试提升对IUSER_ComputerName的访问。 More can be found here . 在这里可以找到更多

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

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