我有一個應用程序,我有一個上傳圖像頁面,我上傳圖像並將這些圖像存儲在圖像文件夾下的不同文件夾及其數據庫中的路徑下。的是,流動是這樣的:如何從根文件夾中刪除文件夾
- 用戶從系統選擇文件
- 用戶添加描述
- 用戶從下拉列表中選擇部,並根據該選擇的圖像被存儲在一個不同的部門的文件夾。
這是我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";
}
}
}
我希望我清楚你們
我怎麼能這樣做?
如果你想刪除圖像 - 爲什麼你還想知道哪個文件夾要保存? –
@MichaelPerrenoud讓我編輯我的問題,我想你得到我想實現的目標 – amitesh
AVOID字符串concatanation在sql !!!! –