首页 新闻 会员 周边 捐助

遍历文件夹UploadFile下面的所有子文件夹,查找到与数据库中对应的图片名称,进行删除?

0
悬赏园豆:20 [已解决问题] 解决于 2009-04-02 09:50

 

/// <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();
}
}

        
问题补充: 我按照eaglet的回答 又进行了修改,之前删除产品的地方有问题,大家再帮忙看看,这样是否正确? 或者给点意见. 我的测试是类别\类别图片\产品\产品图片都删除了的! 欢迎指正批评! altMsg = "类别管理删除成功 !";//提示信息 //根据类别ID(arrCheckBox[i]),查找类别下面的所属产品,得到产品ID(ProductId) string findString = "Select ProductId from sys_Products where TypeId=" + arrCheckBox[i].Trim();//在这里获取的是MenuId DataTable dt = new DataTable(); SqlDataAdapter myAdapter = new SqlDataAdapter(findString, objConn); myAdapter.Fill(dt); //根据产品ID(ProductId),查找图片名称(ImageName) string ProductId; string findImageName; for (int m = 0; m < dt.Rows.Count; m++) { ProductId = dt.Rows[m][0].ToString(); findImageName = "select ImageName from sys_Images where ImageParent=" + ProductId + ""; SqlDataReader imageNameSDR = operate.getDataReader(findImageName); while (imageNameSDR.Read()) { string path2 = Server.MapPath("../../UploadFile/"); string fileName2 = imageNameSDR["ImageName"].ToString();//图片名称 DeleteFile(path2, fileName2, true);//删除硬盘上的相关产品图片 } imageNameSDR.Close(); string delImageName = "delete sys_Images where ImageParent=" + ProductId + "";//删除数据库中的相关图片信息 operate.Insert_Update_Data(delImageName); }
荣耀轩辕的主页 荣耀轩辕 | 初学一级 | 园豆:140
提问于:2009-03-18 11:41
< >
分享
最佳答案
0

        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);

eaglet | 专家六级 |园豆:17139 | 2009-03-18 12:24
其他回答(2)
0

没看懂代码,关注!

子夜星辰 | 园豆:1613 (小虾三级) | 2009-03-18 12:03
0

public void DeleteDirectory(string p_strDirectory)

{

     if(Directory.Exists(p_strDirectory))

     {

            Directory.Delete(p_strDirectory, true);

    }

}

jowo | 园豆:2834 (老鸟四级) | 2009-03-18 13:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册