删除ASP.NET中的文件代码 - 抛出异常 [英] Deletion of file code in ASP.NET - throwing exception

查看:71
本文介绍了删除ASP.NET中的文件代码 - 抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我试图从.aspx表单中动态删除图像文件,但每次都正确检索路径(〜/ img / DisplayItems /Electronics/Computers/D003.png),在执行File.Delete(路径)时 - 它正在添加一条奇怪的路径(C:\Program Files \Common Files \ Micropros Shared \DevServer\10.0 \)之前我给出的路径因此抛出异常:



无法找到路径的一部分'C:\Program Files \Common Files \ Myscoft Shared \ DevServer \10.0 \〜\ img \ DisplayItems \ Electronics \Computersg \'。



请帮我解决这个问题并纠正我哪里出错了。



谢谢



IB

解决方案

在删除时,您的相对路径需要转换为实际路径,因为删除是使用实际路径完成的。



尝试使用在调用delete方法删除它之前,请先调用Server.Mappath(< your relative =path =>)。


Hai



i我在vb中解释你尝试转换你的编码语言



Dim name = Server.MapPath(〜\ KEY \ + stringFileName)



以上Server.MapPath有网站文件夹的根目录,如IIS中的ur网站文件夹

〜\ KEY \ 是roor文件夹里面的文件夹名称

stringFileName是文件名。



在我的网站上有Key文件夹,里面有文件。



如果我在物理上打开它看起来像那个密钥文件夹中的C:\inetpub \wwwroot \Website \ KEY有D003.png文件。




你的代码中的
尝试给出像

Dim path as String = Server.MapPath(〜/ img / DisplayItems / Electronics / Computers / D003.png)

File.Delete(路径)



问候

阿拉汶

Hello,

I am trying to delete an image file dynamically from an .aspx form but everytime though the path is being retrieved correctly (~/img/DisplayItems/Electronics/Computers/D003.png), while executing File.Delete(path) - it is adding a strange path (C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\) before the path given by me and thus throwing exception as:

Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\img\DisplayItems\Electronics\Computersg\'.

Please help me resolve this problem and correct me where i am going wrong.

Thanks

IB

解决方案

While deletion your relative path need to be converted to actual path as deletion is done using actual path.

Try using Server.Mappath("<your relative="" path="">") before you call delete method to delete it.


Hai

i am explain in vb u try to convert ur coding language

Dim name = Server.MapPath("~\KEY\" + stringFileName)

in above Server.MapPath have root of website folder like ur website folder in IIS
"~\KEY\" is folder name inside in roor folder
stringFileName is file name.

in my eg website have Key folder and inside have files.

if i open in physically it look like C:\inetpub\wwwroot\Website\KEY in that key folder have D003.png file.


in ur code try to give like
Dim path as String = Server.MapPath("~/img/DisplayItems/Electronics/Computers/D003.png")
File.Delete(path)

Regards
Aravind


这篇关于删除ASP.NET中的文件代码 - 抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆