如何从根文件夹中删除文件夹

时间:2013-07-01 13:59:33

标签: c# asp.net database image gridview

我有一个应用程序,其中我有一个上传图像页面,我上传图像并将这些图像存储在图像文件夹下的不同文件夹中,并将其存储在数据库中。流程如下:

  1. 用户从系统中选择文件
  2. 用户添加说明
  3. 用户从下拉列表中选择部门,根据该选择,图像存储在不同部门的文件夹中。
  4. 这是我的uploadImage.cs页面代码:

    在这个页面中,首先我们检查我们在Image / Department文件夹下是否有文件夹,如果不是我们在该部门下创建文件夹和存储图像,如果已经在该部门下创建该商店图像

    protected void btnSubmit_Click1(object sender, EventArgs e)
    {
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebGallery"].ConnectionString;
        string Description = tbImageName.Text.Trim();
        string Priority = lblPriority.Text.Trim();
        //Get Filename from fileupload control
        string imgName = fileuploadimages.FileName.ToString();
        //sets the image path
        string imgPath = "Images/Departments/" + "" + ddlDepartment.SelectedValue + "/";
        bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
        if (!IsExists)
            System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
        //then save it to the Folder
        fileuploadimages.SaveAs(Server.MapPath(imgPath + imgName));
        //Open the database connection
        con.Open();
        //Query to insert images name and Description into database
        SqlCommand cmd = new SqlCommand("insert into Images(ImageName, Description, Path, Priority) values (@ImageName, @Description, @Path, @Priority)", con);
        //Passing parameters to query
        cmd.Parameters.AddWithValue("@ImageName", imgName);
        cmd.Parameters.AddWithValue("@Description", Description);
        cmd.Parameters.AddWithValue("@Path", imgPath + imgName);
        cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
        cmd.ExecuteNonQuery();
        //Close dbconnection
        con.Close();
        tbImageName.Text = string.Empty;
    }
    

    在此页面中,我们创建,编辑,更新和删除部门。现在,当用户点击删除按钮时,我想删除该文件夹,以便该文件夹下的所有图像也将被删除。

    我的departmentMaste.cs页面代码:

    protected void BindEmployeeDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from Department_Master", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
    
    }
    
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        BindEmployeeDetails();
    }
    
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //int id = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        string id = gvDetails.DataKeys[e.RowIndex].Values["ID"].ToString();
        TextBox txtDepartment = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDepartment");
        con.Open();
        SqlCommand cmd = new SqlCommand("update Department_Master set DepartmentName='" + txtDepartment.Text + "'where ID=" + id, con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = id + " Details Updated successfully";
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();
    }
    
    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();
    }
    
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
        string id = gvDetails.DataKeys[e.RowIndex].Values["ID"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from Department_Master where ID=" + id, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindEmployeeDetails();
            lblresult.ForeColor = Color.Red;
            lblresult.Text = id + " details deleted successfully";
        }
    }
    
    protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //getting username from particular row
            string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ID"));
            //identifying the control in gridview
            ImageButton lnkbtnresult = (ImageButton)e.Row.FindControl("imgbtnDelete");
            //raising javascript confirmationbox whenver user clicks on link button
            if (lnkbtnresult != null)
            {
                lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + id + "')");
            }
    
        }
    }
    
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
    
            TextBox txtDepartment = (TextBox)gvDetails.FooterRow.FindControl("txtDepartment");
    
            con.Open();
            SqlCommand cmd =
                new SqlCommand("insert into Department_Master values('" + txtDepartment.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
                lblresult.ForeColor = Color.Green;
                lblresult.Text = txtDepartment.Text + " Details inserted successfully";
            }
            else
            {
                lblresult.ForeColor = Color.Red;
                lblresult.Text = txtDepartment.Text + " Details not inserted";
            }
        }
    }
    

    我希望我很清楚你们

    我该怎么做?

2 个答案:

答案 0 :(得分:1)

更新您的RowDeleting事件。

protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
            string id = gvDetails.DataKeys[e.RowIndex].Values["ID"].ToString();



            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from Department_Master where id=" + id, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            DataTable dt = ds.Table[0];
            for (int i = 0; i < dt.rows.count; i++)
            {
                string imgPath = "Images/Departments/" + "" + dt.rows[i]["DepartmentName"] + "/";
                bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
                if (IsExists)
                    System.IO.Directory.Delete(Server.MapPath(imgPath),true);
            }

            con.Close();


            con.Open();
            SqlCommand cmd = new SqlCommand("delete from Department_Master where ID=" + id, con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
                lblresult.ForeColor = Color.Red;
                lblresult.Text = id + " details deleted successfully";
            }
            BindEmployeeDetails();
        }

对于弹出消息,

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //getting username from particular row
        string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ID"));
        //identifying the control in gridview
        ImageButton lnkbtnresult = (ImageButton)e.Row.FindControl("imgbtnDelete");
        //raising javascript confirmationbox whenver user clicks on link button
        if (lnkbtnresult != null)
        {
            lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + id + "');");
        }

    }

}

确保“ConfirmationBox”方法适合提出确认窗口。

要从Images表中删除图像,您应该参考Department_Master表。就像一个名为DepartmentID的列,其外键引用到Images表中。即使每次将记录插入Images表时,也要插入相应的DepartmentID,一旦完成所有这些操作,就可以在Images表上运行delete命令

 SqlCommand cmd = new SqlCommand("Delete from Images where DepartmentID="+id +"; delete from Department_Master where ID=" + id, con);

将此代码添加到aspx页面

<script type="text/javascript">
function ConfirmationBox(id)
{
    return confirm('Are you sure to delete department Id:'+ id +'?' );
}
</script>

答案 1 :(得分:0)

使用GetFiles从目录中获取文件,并使用GetParent从中获取目录 图像。

string dir = Directory.GetParent(photoPathFromDB).FullName;
string[] files = Directory.GetFiles(dir);
 foreach(string file in files)
     //do stuff
相关问题