Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新前删除图像_C#_Asp.net_Image_File Upload - Fatal编程技术网

C# 更新前删除图像

C# 更新前删除图像,c#,asp.net,image,file-upload,C#,Asp.net,Image,File Upload,我喜欢在更新之前删除图像的一种方法,但它仍然不起作用。我的url路径如下所示: string url = img_prev.ImageUrl.ToString(); if (!url.Equals("")) { if (System.IO.File.Exists(url)) { try { System.IO.File.Delete(Request.PhysicalApplicationPath + url);

我喜欢在更新之前删除图像的一种方法,但它仍然不起作用。我的url路径如下所示:

string url = img_prev.ImageUrl.ToString();
if (!url.Equals(""))
{
    if (System.IO.File.Exists(url))
    {
        try
        {
            System.IO.File.Delete(Request.PhysicalApplicationPath + url);
        }
        catch (System.IO.IOException ex)
        {
            Console.WriteLine(ex.Message);
            return;
        }
    }

我怀疑您正在使用web URL作为参数,该参数不适用于
System.IO.File.Exists()
,请改用此参数:

../uploadimages/21-2-2013-14-55-49-P1120011.JPG

我怀疑在您尝试删除图像时,该图像正在使用中,这就是无法删除的原因。虽然url获取路径图像,但当我在这行中设置错误时,如果(System.IO.File.Exists(url)),则返回false
System.IO.File.Exists(string path)
在文件不存在或路径无效时返回false,这就是为什么我假设您的
url
无效(它是路径(
c:\somePath
)还是真正的url(
www.someURL.com
)?
if (File.Exists(Server.MapPath(filePath)))