我把文件的相对路径保存在数据库里,当我要删除某个文件的时候,从数据库里取出相对路径。然后这样删除:
File.Delete(Server.MapPath(路径));
但报错,未找到路径“D:\kpapa\manage\associator\123456\music\20060714101041993.wma”的一部分。 
请问各位朋友,该怎样写这句代码呢?

解决方案 »

  1.   

    你先用File.Exists检查文件是否存在,然后整个过程用try catch来捕获异常
      

  2.   

    if File.Exists(Server.MapPath(路径))
    {
    try
    {
    File.Delete(Server.MapPath(路径));
    }
    catch(exception e)
    {
     throw e
    }
    }
      

  3.   

    string fullPath = Server.MapPath(路径);
    if(File.Exists(fullPath))
    {
        File.Delete(fullPath);
    }
      

  4.   

    保存在数据库里的路径是:associator/123456/music/20060714021334159.wma我需要把20060714021334159.wma这个文件删了。我就先把数据库里的路径的值赋给了一个变量path,然后:File.Delete(Server.MapPath(path));但报错:未找到路径“D:\kpapa\manage\associator\123456\music\20060714101041993.wma我很菜,各位朋友帮帮忙,这个问题我搞了很久了。
      

  5.   

    上面贴错了
    ---------------------------------------------------------------------
    保存在数据库里的路径是:associator/123456/music/20060714021334159.wma我需要把20060714021334159.wma这个文件删了。我就先把数据库里的路径的值赋给了一个变量path,然后:File.Delete(Server.MapPath(path));但报错:未找到路径“D:\kpapa\manage\associator\123456\music\20060714021334159.wma我很菜,各位朋友帮帮忙,这个问题我搞了很久了。
      

  6.   

    仔细检查一下,真的有可能是路径错误。比方说路径可能是../associator/123456/music/20060714021334159.wma呵呵,个人拙见。
      

  7.   

    把File.Delete(Server.MapPath(path));放在条件语句里
    if(File.Exists(Server.MapPath(path)))
    {
    File.Delete(Server.MapPath(path));
    }
    else
    {
    //抛错;
    }
    看下是不是真的没有文件
      

  8.   

    果然是路径的问题,在项目文件kpapa下有:associator\123456\music\20060714021334159.wma
    我数据库里保存的路径是:associator/123456/music/20060714021334159.wma
    我把后台页面放在项目文件kpapa下的manage文件夹里,Server.MapPath(path)这样做就把绝对路径弄错了,请问该怎么做,Server.MapPath(path)才会返回正确的绝对路径呢?
      

  9.   

    Request.PhysicalApplicationPath + "\\" + path