如何从根文件夹中删除文件夹 [英] how to delete folder from a root folder

查看:116
本文介绍了如何从根文件夹中删除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



用户从系统中选择文件

用户添加说明

用户从下拉列表中选择部门,根据该选择,图像存储在不同部门的文件夹中。



这是我的uploadImage.cs页面代码:



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





I have an application in which I have an upload image page where I upload images and store those images in different folders under image folder and its path in a database. The flow of that goes like this:

user selects file from the system
user add description
user selects department from the drop-down list and according to that selection the image is stored in a different department's folder.

this is my uploadImage.cs page code:

In this page first we check that we have folder under Image/Department folder or not if not than we create folder and store image under that department else if already create that store image under that 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;
}







In this page we create,edit,update and delete department. Now when user click on delete button i want to delete that folder so that all the image under that folder will also delete.

My departmentMaste.cs page code:










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





i希望我们很清楚你们



我该怎么办?



i hope i am clear to you guys

How can I do that?

推荐答案

我可以从你的解释中了解到,你要删除所有的目录中的文件以及该目录中的所有文件。



如果以上是正确的;以下可以是方法:

获取服务器地图路径URL并单独获取文件夹。然后遍历下面的代码并删除文件



As I can understand from your explanation is that, you want to delete all the files inside a directory along with all the files inside that directory.

If above is correct; following can be the approach:
Get the server map path url and get the folder alone. then loop through the below code and delete the files

DirectoryInfo di = new DirectoryInfo(PhysicalDirectoryPath); 
FileInfo[] rgFiles = di.GetFiles("*.aspx"); //For specific extensions or user GetFiles() for all the files.
foreach(FileInfo fi in rgFiles) 
{ 
// Delete the files here 
}

Once all the files are deleted, delete the directory as well

di .Delete();
Label2.Text = "Directory deleted successfully!";  

希望这会对你有所帮助



~Amol

Hope this will help you

~Amol


这是一些帮助链接尝试这些。希望你能轻松找到解决方案。



Directory.Delete方法(字符串,布尔值)



如何:复制,删除和移动文件和文件夹(C#编程指南)



删除指定的文件。
Here is some helping links try these. hope so you can find your solution easily.

Directory.Delete Method (String, Boolean)

How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

Deletes the specified file.


我想这会对你有所帮助.....根据你的需要修改代码...... :)





I think this will help you.....Modify code as per your need...:)


FileInfo[] fileList;
        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("Directory Path"));
        fileList = dir.GetFiles();
        for (int i = 0; i < fileList.Length; i++)
        {
            fileList[i].Delete();
        }


这篇关于如何从根文件夹中删除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆