//相对来讲删除文件
1、直接上代码
public bool DeleteFile(string path)
{
try
{
string paths = Server.MapPath(path);
bool sta = System.IO.File.Exists(paths);
if (sta)
{
System.IO.File.Delete(paths);
return true;
}
else {
return false;
}
}
catch //(Exception e)
{
//throw e;
return false;
}
finally {
}
}
.
2、这些为异常处理
try{}
catch (Exception e){}
finally {}
3、核心代码:
利用IO流删除文件
第一步 > string paths = Server.MapPath(“输入相对路径”); //通过先对路径获得该文件的绝对路径
第二步 > System.IO.File.Exists(paths); //判断该路径是否存在 返回bool值
第三步 > System.IO.File.Delete(paths); //将绝对路径传入Delete方法即可删除该文件
ps:相对路径是相对一个某个文件存在的位置 一般为 /Images/ions.png
绝对路径是从电脑硬盘的根目录开始的路径 一般为 D:\工具、资料\代码工具\MVC 入门知识
("../"返回上一级目录;"~"当前目录的根目录;)
System.IO.File.Delete(paths);