2013-07-01 96 views
0

我有一個應用程序,我有一個上傳圖像頁面,我上傳圖像並將這些圖像存儲在圖像文件夾下的不同文件夾及其數據庫中的路徑下。的是,流動是這樣的:如何從根文件夾中刪除文件夾

  1. 用戶從系統選擇文件
  2. 用戶添加描述
  3. 用戶從下拉列表中選擇部,並根據該選擇的圖像被存儲在一個不同的部門的文件夾。

這是我uploadImage.cs頁面代碼:

在該頁面中我們首先檢查,我們有文件夾下的圖片/部門文件夾或不如果不是我們該部門否則,如果下創建文件夾並存儲圖像已經在該部門下創建該商店圖像

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"; 
     } 
    } 
} 

我希望我清楚你們

我怎麼能這樣做?

+0

如果你想刪除圖像 - 爲什麼你還想知道哪個文件夾要保存? –

+0

@MichaelPerrenoud讓我編輯我的問題,我想你得到我想實現的目標 – amitesh

+0

AVOID字符串concatanation在sql !!!! –

回答

0

使用GetFiles從目錄中獲取文件並使用GetParent從 獲取目錄的圖像。

string dir = Directory.GetParent(photoPathFromDB).FullName; 
string[] files = Directory.GetFiles(dir); 
foreach(string file in files) 
    //do stuff 
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的Column,其外鍵參考Images表。甚至當你插入記錄Images表中,還插入相應的DepartmentID,一旦你完成所有的這些東西,你可以運行在Images表中刪除命令

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> 
+0

它不會從網格視圖刪除部門,如果任何文件夾有一些圖像,那麼它會顯示錯誤消息,該文件夾不是空的 – amitesh

+0

該目錄不是空的 – amitesh

+0

好的,我已經更新了我的答案。我做了2個修改。1)在Delete方法中添加了一個參數爲true - 這將有助於刪除該文件夾,儘管它不是空的,並且2)最後一行將網格再次與更新後的數據綁定。請參閱http://msdn.microsoft.com/en-us/library/fxeahc5f(v=vs.100).aspx – Bhaarat