/// <summary>
/// 通过复选类别ID,更新类别管理的排序和可见性;删除类别及其所有子类别;删除类别下面所包含的所有产品;删除类别下面所包含产品的相关图片
/// </summary>
private void update_delete_Menu()
{
string strCheckBox, strOrder, strSQL = "", strVisible = "0", strSelectImage = "", strDelImage = "", altMsg = "";
string[] arrCheckBox ={ "" };
string intype = Request.Form["intype"].ToString();
strCheckBox = Request.Form["MenuId"];//checkbox控件
if (strCheckBox == null) strCheckBox = "";
if (strCheckBox != "")
{
strCheckBox = strCheckBox.Trim();
arrCheckBox = strCheckBox.Split(',');
for (int i = 0; i < arrCheckBox.Length; i++)
{
strOrder = Request.Form["order_" + arrCheckBox[i].Trim()];
if (strOrder == null || strOrder == "") strOrder = "0";
strVisible = Request.Form["Visible_" + arrCheckBox[i].Trim()];
if (strVisible == null || strVisible == "") strVisible = "0";
if (intype != null)
{
if (intype == "update")
{
altMsg = "类别管理更新成功 !";
strSQL = "Update sys_Menu set MenuOrder='" + strOrder + "',Visible=" + strVisible + " where MenuId=" + arrCheckBox[i].Trim();
operate.Insert_Update_Data(strSQL);
}
if (intype == "delete")
{
altMsg = "类别管理删除成功 !";
SqlCommand cmd = new SqlCommand("Products_Delete", objConn);//根据传进去的TypeId删除其下所有的产品
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter TypeId = new SqlParameter("@typeid", SqlDbType.Int);
TypeId.Direction = ParameterDirection.Input;
TypeId.Value = arrCheckBox[i].Trim();
cmd.Parameters.Add(TypeId);
SqlParameter outParameter = new SqlParameter("@execResult", SqlDbType.Int);
outParameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(outParameter);
objConn.Open();
cmd.ExecuteNonQuery();
objConn.Close();
int j = Convert.ToInt32(cmd.Parameters["@execResult"].Value.ToString());
if (j == 1)
{
strSQL = "Delete sys_Menu where MenuId=" + arrCheckBox[i].Trim();
operate.Insert_Update_Data(strSQL);
}
else
{
Response.Write(operate.ShowMsgBox("类别管理删除失败 !", "categoryList.aspx?CMid=" + CMid + "&MNid=" + MNid));
Response.End();
}
strSelectImage = "select * from sys_Images where ImageParent=" + arrCheckBox[i].Trim();//ImageParent为所属类别ID
SqlDataReader imageSDR = operate.getDataReader(strSelectImage);
while (imageSDR.Read())
{
string imgPath = Server.MapPath("../../UploadFile/Menus/") + imageSDR["ImageName"].ToString();//在这里Menus还可能是Products,News,Downloads,Links,Others,Editors,应该如何查找并删除????????????
System.IO.FileInfo imgfile = new System.IO.FileInfo(imgPath);
if (imgfile.Exists)// 判断路径名是否存在
{
imgfile.Delete();//如果存在,先删除
}
}
imageSDR.Close();
strDelImage = "delete sys_Images where ImageParent=" + arrCheckBox[i].Trim();
operate.Insert_Update_Data(strDelImage);
}
}
}
Response.Write(operate.ShowMsgBox(altMsg, "categoryList.aspx?CMid=" + CMid + "&MNid=" + MNid));
Response.End();
}
}
static void DeleteFile(string path, string fileName, bool recursive)
{
if (path[path.Length - 1] != '\\')
{
path += '\\';
}
if (!recursive)
{
System.IO.File.Delete(path + fileName);
}
else
{
System.IO.File.Delete(path + fileName);
string[] subFolders = System.IO.Directory.GetDirectories(path);
foreach (string folder in subFolders)
{
DeleteFile(folder, fileName, recursive);
}
}
}
上面代码当 recursive 传入为 true 时删除 path 目录下所有子目录中文件名为 fileName的文件
针对你的代码,如下修改:
sring path = Server.MapPath("../../UploadFile/") ;
string fileName = imageSDR["ImageName"].ToString();
DeleteFile(path, fileName, true);
没看懂代码,关注!
public void DeleteDirectory(string p_strDirectory)
{
if(Directory.Exists(p_strDirectory))
{
Directory.Delete(p_strDirectory, true);
}
}