C#删除文件

/// <summary>
/// 删除文件
/// </summary>
/// <param name="fileUrl">路径</param>
public string DeleteImgFile(string id, string url)
{
    var obj = new object { };
    try
    {
        string file = System.Web.HttpContext.Current.Server.MapPath(url);
        if (System.IO.File.Exists(file))
        {
            System.IO.File.Delete(file);
            HealthCls cls = new HealthCls();
            return cls.DeleteImgFile(id, url);
        }
        else
        {
            obj = new { errCode = 0, errMessage = "成功!" };
            return JsonConvert.SerializeObject(obj);
        }
    }
    catch (Exception ex)
    {
        obj = new { errCode = 1, errMessage = ex.Message };
        return JsonConvert.SerializeObject(obj);
    }
}

 

posted @ 2021-05-24 17:32  时光博客  阅读(238)  评论(0编辑  收藏  举报