我有一个带有实体框架的mvc 4应用程序。
我想删除一个文件,但每次它都说:
mscorlib.dll中发生类型为“System. UnauthorizedException”的异常,但未在用户代码中处理
附加信息:拒绝访问路径“G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\”。
这一行:System.IO.File.Delete(path);
这就是方法:
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");
}
字符串
我只是在Visual Studio中运行应用程序,如下所示:http://localhost:41787/Account/Edit?UserId=hallo
我已经做了以下事情:
完全访问Map,我添加了网络服务到Map与完全控制.但似乎什么都不工作.我使用windows 7.我运行Visual Studio 2013作为管理员
我也看到了这一点:
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.
型
在这里你可以看到访问:
x1c 0d1x的数据
我试着这样做:
<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>
型
我添加了这个:
<identity impersonate="true" userName="Administrator" password="windowsPassword"/>
型
好吧,我可以运行应用程序,但仍然给出错误:
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:
型
完全许可:
的
高级选项卡:
更改的权限选项卡:
的
我把我的操作方法编辑成这样:
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");
}
型
现在它起作用了!
3条答案
按热度按时间tjvv9vkg1#
我也有这个问题,因此我在这篇文章上跌跌撞撞。我在复制/删除之前和之后添加了以下代码行。
删除
字符串
副本
型
mznpcxlj2#
基于答案-对我来说,我必须将文件夹 * 和其中的文件 * 设置为正常属性。
字符串
uqdfh47h3#
foreach(string temp_dir in Directory.GetDirectories(temp_root)){
字符串