简体   繁体   English

正在删除文件,但访问被拒绝

[英]Deleting file, but is access denied

I have an mvc4 application with entity framework.我有一个带有实体框架的 mvc4 应用程序。

I want to delete a file, but every time it says:我想删除一个文件,但每次它都说:

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code mscorlib.dll 中出现“System.UnauthorizedAccessException”类型的异常,但未在用户代码中处理

Additional information: Access to the path 'G:\\Mijn Documents\\My Web Sites\\Lolabikes - Copy\\C#\\ContosoUniversity\\Images\\' is denied.附加信息:拒绝访问路径“G:\\Mijn Documents\\My Web Sites\\Lolabikes - Copy\\C#\\ContosoUniversity\\Images\\”。

by this line: System.IO.File.Delete(path);通过这一行: System.IO.File.Delete(path);

This is the method:这是方法:

public ActionResult DeleteFiles(int id)
        {           
                //var fileName = Path.GetFileName(id.FileName);

                var DirSeparator = Path.DirectorySeparatorChar;
                var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
               var file = db.lolabikerPhotos.Find(id);               
               System.IO.File.Delete(path);
               db.SaveChanges();           

            return Redirect(Url.Action("Edit", "Account") + "#tabs-3");

        }

I just run the app in visual studio, like this: http://localhost:41787/Account/Edit?UserId=hallo我只是在 Visual Studio 中运行该应用程序,如下所示: http://localhost:41787/Account/Edit?UserId=hallo

I have done already the following things:我已经做了以下事情:

Full access to the map, I added the Network Service to the map with full control.对地图的完全访问权限,我将网络服务添加到完全控制的地图中。 But nothing seems to work.但似乎没有任何效果。 I am using windows 7. And I run visual studio 2013 as administrator我使用的是 Windows 7。我以管理员身份运行 Visual Studio 2013

I also see this:我也看到这个:

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

Here you can see the access:在这里你可以看到访问:

在此处输入图片说明

I try something like this:我尝试这样的事情:

 <system.web>

    <identity impersonate="true" userName="Administrator" password="windowsPassword"/>
    <httpRuntime requestValidationMode="2.0"  maxRequestLength="1048576" executionTimeout="3600" />
    <compilation debug="true" targetFramework="4.5" />

    <pages validateRequest="false" />

    <!--<httpRuntime targetFramework="4.5" />-->


  </system.web>

I've added this:我添加了这个:

<identity impersonate="true" userName="Administrator" password="windowsPassword"/> 

OK, I can run the app, but still gives the error:好的,我可以运行该应用程序,但仍然出现错误:

Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.UnauthorizedAccessException: Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied. 

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error: 


Line 489:                var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
Line 490:               var file = db.lolabikerPhotos.Find(id);               
Line 491:               System.IO.File.Delete(path);
Line 492:               db.SaveChanges();           
Line 493:

full permission:完全许可:

在此处输入图片说明

Advanced tab:高级选项卡:在此处输入图片说明

Changed permissions tab:更改权限选项卡:

在此处输入图片说明

I edited my action method like this:我像这样编辑了我的操作方法:

 public ActionResult DeleteFiles(int id)
        {           
                var fileName = Path.GetFileName(@"\\Koala.jpg");

                var DirSeparator = Path.DirectorySeparatorChar;
                var path = Server.MapPath(@"\\Images" + DirSeparator + fileName.Replace('+', '_'));
               var file = db.lolabikerPhotos.Find(id);
               LolaBikePhoto lola = db.lolabikerPhotos.Find(id);
               db.lolabikerPhotos.Remove(lola);
               System.IO.File.Delete(path);


               db.SaveChanges();           

            return Redirect(Url.Action("Edit", "Account") + "#tabs-3");

        }

And now it is working!现在它正在工作!

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);

Building upon the answer - For me I had to set the folder and the files inside it to normal attributes.基于答案 - 对我来说,我必须将文件夹和其中的文件设置为正常属性。

    DirectoryInfo directory = new DirectoryInfo("/path/to/file");
    directory.Attributes = FileAttributes.Normal;

    foreach (FileInfo file in directory.GetFiles()) {
        file.Attributes = FileAttributes.Normal;
    }

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

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