日期:2014-05-18  浏览次数:20372 次

请高手指点:文件删除是使用相对路径还是绝对路径呢?File.Delete的问题
我的代码是System.IO.File.Delete(this.Server.MapPath( ".. ")+@ "/VideoDemo/Video/ "+fileName); 不知道这样是否是正确的?


还有判断文件存在的问题,为什么也是要用绝对路径,可以用相对路径解决吗??如
if(System.IO.File.Exists(this.Server.MapPath( ".. ")+@ "\VideoDemo\Video\ "+fileName)==false)

因为我怕别人访问时访问不了呀?

------解决方案--------------------
都用绝对路径吧
另外,建议用System.IO.Path.Combine来拼凑文件名
这样可以减少\出现丢失或重复的问题
------解决方案--------------------
建议使用相对路径,否则放到主机提供商那里路径会不一样的
------解决方案--------------------
路径可以保存在webconfig中
------解决方案--------------------
可以用相对路径。
string picturePath = GetPicturePath(goods_id);
if (File.Exists(this.Server.MapPath(picturePath)))
{
File.Delete(this.Server.MapPath(picturePath));
}


protected string GetPicturePath(int goods_id)
{
BLL.zqu_goods_goods bll = new zqu_goods_goods();
DataSet ds = bll.GetList(" goods_id='" + goods_id + "'");
string picPath = "~/uploadfile/" + ds.Tables[0].Rows[0][10].ToString();
return picPath;
}
------解决方案--------------------
物理觉得路径。
例如:
c:\www\1.txt
------解决方案--------------------
同意楼上的
可以用相对路径。 
string path = Server.MapPath("~/UploadImages/fwys/");
string file = path + filename;
if (File.Exists(file))
{
File.Delete(file);
Common.Message(this,"附件删除成功!");
}

------解决方案--------------------
探讨
同意楼上的
可以用相对路径。
string path = Server.MapPath("~/UploadImages/fwys/");
string file = path + filename;
if (File.Exists(file))
{
File.Delete(file);
Common.Message(this,"附件删除成功!");
}

------解决方案--------------------
汗,显然没有明白原理.操作时所有路径都是绝对的.
Server.MapPath()就是获取绝对路径,你要把文件放在IIS有权限访问的目录下(一般在web目录下就可以,要删除的话就必须要有写权限).
所以,你所谓的担心是多余也是没用的.你上面的拼接也是多此一举.