locked
Get Sub Folders and Filenames From Main Folder in Gridview Control in ASP.Net C# RRS feed

  • Question

  • User1872364149 posted

    Hello all,

    I need shows the subdirectory and file names from a subdirectory into a GridView control.

    On the server I have this folder structure :

    MainFolder 
     1_Subfolder 
     2_Subfolder 
     3_Subfolder
    

    For each SubFolder I have variable number of files.

    I need first show the MainFolder and click on MainFolder show the Subfolder and click on the Subfolder show all files.

    If click on the file name I need the download of this file.

    I thinked use the Directory class but without success, because this code :

            DirectoryInfo directory = new DirectoryInfo(@FilePath);
            DirectoryInfo[] subDirectories = directory.GetDirectories();
    
            foreach (DirectoryInfo dirInfo in subDirectories)
            {
                gvDownload.DataSource = subDirectories;
                gvDownload.DataBind();
            }

    Show these all header columns :

    Name, FullName, Exists, Extension, CreationTime, CreationTimeUtc, LastAccessTime, LastAccessTimeUtc, LastWriteTime, LastWriteTimeUtc 

    And I need only :

    Name, 
    Size (KB), 
    Number subfolders, 
    File content, 
    Date last edit

    Can you help me?

    Thank you in advance for any help, really appreciated.

    Wednesday, October 11, 2017 9:35 AM

Answers

  • User-1838255255 posted

    Hi cms9651,

    Based on your code, GetData method return the orginal datatable.

    In fact, you should merge the orginal datatable and DirectoryInfo into a new datatable, and return this new datatable.

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 18, 2017 9:22 AM
  • User-271186128 posted

    Hi cms9651,

    For each SubFolder I have variable number of files.

    I need first show the MainFolder and click on MainFolder show the Subfolder and click on the Subfolder show all files.

    As for this issue, you can access file system information by using the following classes

    More details, you could check the following sample code:

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $("[src*=plus]").live("click", function () {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
                $(this).attr("src", "images/minus.png");
            });
            $("[src*=minus]").live("click", function () {
                $(this).attr("src", "images/plus.png");
                $(this).closest("tr").next().remove();
            });
        </script>
        <style type="text/css">
            body {
                font-family: Arial;
                font-size: 10pt;
            }
    
            .Grid td {
                background-color: #A1DCF2;
                color: black;
                font-size: 10pt;
                line-height: 200%;
            }
    
            .Grid th {
                background-color: #3AC0F2;
                color: White;
                font-size: 10pt;
                line-height: 200%;
            }
    
            .ChildGrid td {
                background-color: #eee !important;
                color: black;
                font-size: 10pt;
                line-height: 200%;
            }
    
            .ChildGrid th {
                background-color: #6C6C6C !important;
                color: White;
                font-size: 10pt;
                line-height: 200%;
            }
        </style>
    
            <div>
                <asp:GridView ID="gvDic" AutoGenerateColumns="false" CssClass="Grid" OnRowDataBound="gvDic_RowDataBound" runat="server">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <img alt="" style="cursor: pointer" src="Images/plus.png" />
                                <asp:Panel ID="pnlSubDic" runat="server" Style="display: none">
                                    <asp:GridView ID="gvSubDic" runat="server" OnRowDataBound="gvSubDic_RowDataBound" AutoGenerateColumns="false" CssClass="ChildGrid">
                                        <Columns>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:HiddenField ID="hidPath" runat="server" Value='<%# Eval("Path") %>' />
                                                    <img alt="" style="cursor: pointer" src="Images/plus.png" />
                                                    <asp:Panel ID="pnlSubDic" runat="server" Style="display: none">
                                                        <asp:GridView ID="gvFile" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
                                                            <Columns>
                                                                <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
                                                                <asp:BoundField ItemStyle-Width="150px" DataField="CreateTime" HeaderText="CreateTime" />
                                                                <asp:BoundField ItemStyle-Width="150px" DataField="Size" HeaderText="Size" />
                                                            </Columns>
                                                        </asp:GridView>
                                                    </asp:Panel>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="CreateTime" HeaderText="CreateTime" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="Size" HeaderText="Size" />
                                        </Columns>
                                    </asp:GridView>
                                </asp:Panel>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                <asp:HiddenField ID="hidPath" runat="server" Value='<%# Eval("Path") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="CreateTime">
                            <ItemTemplate>
                                <asp:Label ID="lblCreateTime" runat="server" Text='<%# Eval("CreateTime") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="Size">
                            <ItemTemplate>
                                <asp:Label ID="lblSize" runat="server" Text='<%# Eval("Size") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>

    Code in aspx.cs:

       public partial class WebForm3 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string[] folderlist = Directory.GetDirectories(Server.MapPath("Upload"));
    
                List<vmFile> filelist = new List<vmFile>();
                foreach(var item in folderlist)
                {
                    vmFile file = new vmFile();
                    file.Name = Path.GetFileName(item);
                    file.Path = item;
                    file.CreateTime = File.GetLastWriteTime(item);
                    file.Size = GetDirectorySize(item);
                    filelist.Add(file);
                }
    
                gvDic.DataSource = filelist;
                gvDic.DataBind();
            }
    
            protected void gvDic_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
    // use a hiddenfield to store the file or folder path HiddenField hidpath = (HiddenField)e.Row.FindControl("hidPath"); if (hidpath != null) { string[] subfolderlist = Directory.GetDirectories(hidpath.Value); List<vmFile> filelist = new List<vmFile>(); foreach (var item in subfolderlist) { vmFile file = new vmFile(); file.Name = Path.GetFileName(item); file.Path = item; file.CreateTime = File.GetLastWriteTime(item); file.Size = GetDirectorySize(item); filelist.Add(file); } GridView gvChild = (GridView)e.Row.FindControl("gvSubDic"); if (gvChild != null) { gvChild.DataSource = filelist; gvChild.DataBind(); } } } } protected void gvSubDic_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HiddenField hidpath = (HiddenField)e.Row.FindControl("hidPath"); if (hidpath != null) { DirectoryInfo di = new DirectoryInfo(hidpath.Value); List<vmFile> filelist = new List<vmFile>(); //get files foreach (var item in di.GetFiles()) { vmFile file = new vmFile(); file.Name = item.Name; file.CreateTime = item.LastWriteTime; file.Size = Math.Round((double)item.Length / (double)(1024), 2).ToString() + " KB"; filelist.Add(file); } GridView gvChild = (GridView)e.Row.FindControl("gvFile"); if (gvChild != null) { gvChild.DataSource = filelist; gvChild.DataBind(); } } } } //get the directory size private string GetDirectorySize(string path) { DirectoryInfo info = new DirectoryInfo(path); long size = 0; foreach (string file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)) { size += new FileInfo(file).Length; } return Math.Round((double)size / (double)(1024), 2).ToString() + " KB"; } } //define a model, used to store the file or folder information public class vmFile { public string Name { get; set; } public DateTime CreateTime { get; set; } public string Size { get; set; } public string Path { get; set; } }

    The folder as below:

    The screenshot as below:

    Reference:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree 

    If click on the file name I need the download of this file.

    If you want to download the file, you could use the LinkButton control display the file name. And refer to the following link to download file:

    https://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 24, 2017 5:24 AM

All replies

  • User-1838255255 posted

    Hi cms9651,

    According to your description, I think you could refer to this article to read then folder and file properties, then save them in the database:

    Directory.GetDirectories Method (String):

    https://msdn.microsoft.com/en-us/library/c1sez4sc(v=vs.110).aspx 

    About you want to show folder and all files of this folder in gridview, please check this article:

    Nested GridView Example in ASP.Net using C# and VB.Net:

    https://www.aspsnippets.com/Articles/Nested-GridView-Example-in-ASPNet-using-C-and-VBNet.aspx 

    Best Regards,

    Eric Du

    Thursday, October 12, 2017 8:12 AM
  • User1872364149 posted

    Hi, thank you for reply.

    I have tried your article, but I can't see the Name of subfolder.

    The error is :

    A field or property with the name 'Name' was not found on the selected data source
    
    

    Can you help me ?

    My code below.

    Thank you in advance for any help,really appreciated.

    .cs

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            gvCustomers.DataSource = GetData(sql, dirInfo, subDirectories);
            gvCustomers.DataBind();
            }
        }
    
    
    
        private static DataTable GetData(string sql, DirectoryInfo dirInfo, DirectoryInfo[] subDirectories)
        {
            sql = @String.Format(" SELECT * FROM doTable; ");
    
            using (OdbcConnection con =
              new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
            {
                using (OdbcCommand cmd = new OdbcCommand())
                {
    
                    cmd.CommandText = sql;
                    using (OdbcDataAdapter sda = new OdbcDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
    
                        using (DataSet ds =
                            new DataSet())
                        {
                            DataTable dt =
                                new DataTable();
    
                            sda.Fill(ds);
                            sda.Fill(dt);
    
                            dt = ds.Tables[0];
                            string FilePath = HttpContext.Current.Server.MapPath("/myfolder/" + ds.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\'));
                            HttpContext.Current.Response.Write(FilePath);
    
                            dirInfo = new DirectoryInfo(@FilePath);
                            subDirectories = dirInfo.GetDirectories();
    
                            return dt;
                        }
                    }
                }
            }
        }

    .aspx

                        <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="mGrid" Width="500" HorizontalAlign="Center"
                            DataKeyNames="ID" OnRowDataBound="OnRowDataBound">
                            <Columns>
    
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <img alt="" style="cursor: pointer" src="images/plus.png" />
                                        <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                                            <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="mGrid" Width="500" HorizontalAlign="Center">
                                                <Columns>
                                                    <asp:BoundField DataField="OrderId" HeaderText="Order Id" />
                                                    <asp:BoundField DataField="OrderDate" HeaderText="Date" />
                                                </Columns>
                                            </asp:GridView>
                                        </asp:Panel>
                                    </ItemTemplate>
                                </asp:TemplateField>
    
                                <asp:BoundField DataField="Name" HeaderText="SubFolder" />
    
                            </Columns>
                        </asp:GridView>

    Friday, October 13, 2017 9:31 AM
  • User-1838255255 posted

    Hi cms9651,

    According to your description, I am not clear which line code caused this issue, I notice that you use BoundField DataField="Name", please check the return data from the database if contain Name, check the header if is same as header in database? I hope you could provide more detailed description, this will better help you solve the problem.

    Best Regards,

    Eric Du

    Tuesday, October 17, 2017 11:38 AM
  • User1872364149 posted

    Hi Eric, thank you for reply.

    The `Name` in the BoundField is property of dirInfo Directory.GetDirectories Method and don't is contain in in database.

    My problem is :

    1. read the path of the folder stored in the database ===> string FilePath;
    2. show in the GridView on the BoundField the subfolders contained on the path of the folder stored in the database  ===> string FilePath

    All the best

    Cheers, 
    Chevy Mark Sunderland (cms)

    Tuesday, October 17, 2017 1:28 PM
  • User-1838255255 posted

    Hi cms9651,

    Based on your code, GetData method return the orginal datatable.

    In fact, you should merge the orginal datatable and DirectoryInfo into a new datatable, and return this new datatable.

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 18, 2017 9:22 AM
  • User1872364149 posted

    Hi Eric Du, thank you for reply.

    Two datatable in the same GridView ?

    It's possible ?

    Do you have any code example ?

    Thursday, October 19, 2017 7:06 AM
  • User1872364149 posted

    Eric Du

    Hi cms9651,

    Based on your code, GetData method return the orginal datatable.

    In fact, you should merge the orginal datatable and DirectoryInfo into a new datatable, and return this new datatable.

    Best Regards,

    Eric Du

    Hi Eric, thank you for reply.

    This is my new code, I don't have error but I can't show only the files in subdirectory.

    In GridView for any subdirectory I have all files and not separated for subdirectory.

                <asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
                    <AlternatingRowStyle BackColor="White" />
                    <EditRowStyle BackColor="#7C6F57" />
                    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#E3EAEB" />
                    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F8FAFA" />
                    <SortedAscendingHeaderStyle BackColor="#246B61" />
                    <SortedDescendingCellStyle BackColor="#D4DFE1" />
                    <SortedDescendingHeaderStyle BackColor="#15524A" />
                    <Columns>
    
                        <asp:TemplateField HeaderText="N." ItemStyle-CssClass="ddl_Class_new" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <%#Container.DataItemIndex + 1%>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField>
                            <ItemTemplate>
                                <img alt="" style="cursor: pointer" src="images/plus.png" />
                                <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="mGrid" Width="500" HorizontalAlign="Center"
                                        OnRowDataBound="OnRowDataBound">
                                        <Columns>
                                            <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="File Name" />
                                        </Columns>
                                    </asp:GridView>
                                </asp:Panel>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:BoundField DataField="Name" HeaderText="Folder name" />
    
                    </Columns>
                </asp:GridView>
    
    
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
    DirectoryInfo directory = new DirectoryInfo(@FilePath);
    DirectoryInfo[] subDirectories = directory.GetDirectories();
    FileInfo[] fileInfo = directory.GetFiles("*.*", SearchOption.AllDirectories);
    gvOrders.DataSource = fileInfo;
    gvOrders.DataBind();
    }
    }

    Thursday, October 19, 2017 10:53 AM
  • User-271186128 posted

    Hi cms9651,

    For each SubFolder I have variable number of files.

    I need first show the MainFolder and click on MainFolder show the Subfolder and click on the Subfolder show all files.

    As for this issue, you can access file system information by using the following classes

    More details, you could check the following sample code:

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $("[src*=plus]").live("click", function () {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
                $(this).attr("src", "images/minus.png");
            });
            $("[src*=minus]").live("click", function () {
                $(this).attr("src", "images/plus.png");
                $(this).closest("tr").next().remove();
            });
        </script>
        <style type="text/css">
            body {
                font-family: Arial;
                font-size: 10pt;
            }
    
            .Grid td {
                background-color: #A1DCF2;
                color: black;
                font-size: 10pt;
                line-height: 200%;
            }
    
            .Grid th {
                background-color: #3AC0F2;
                color: White;
                font-size: 10pt;
                line-height: 200%;
            }
    
            .ChildGrid td {
                background-color: #eee !important;
                color: black;
                font-size: 10pt;
                line-height: 200%;
            }
    
            .ChildGrid th {
                background-color: #6C6C6C !important;
                color: White;
                font-size: 10pt;
                line-height: 200%;
            }
        </style>
    
            <div>
                <asp:GridView ID="gvDic" AutoGenerateColumns="false" CssClass="Grid" OnRowDataBound="gvDic_RowDataBound" runat="server">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <img alt="" style="cursor: pointer" src="Images/plus.png" />
                                <asp:Panel ID="pnlSubDic" runat="server" Style="display: none">
                                    <asp:GridView ID="gvSubDic" runat="server" OnRowDataBound="gvSubDic_RowDataBound" AutoGenerateColumns="false" CssClass="ChildGrid">
                                        <Columns>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:HiddenField ID="hidPath" runat="server" Value='<%# Eval("Path") %>' />
                                                    <img alt="" style="cursor: pointer" src="Images/plus.png" />
                                                    <asp:Panel ID="pnlSubDic" runat="server" Style="display: none">
                                                        <asp:GridView ID="gvFile" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
                                                            <Columns>
                                                                <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
                                                                <asp:BoundField ItemStyle-Width="150px" DataField="CreateTime" HeaderText="CreateTime" />
                                                                <asp:BoundField ItemStyle-Width="150px" DataField="Size" HeaderText="Size" />
                                                            </Columns>
                                                        </asp:GridView>
                                                    </asp:Panel>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="CreateTime" HeaderText="CreateTime" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="Size" HeaderText="Size" />
                                        </Columns>
                                    </asp:GridView>
                                </asp:Panel>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                <asp:HiddenField ID="hidPath" runat="server" Value='<%# Eval("Path") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="CreateTime">
                            <ItemTemplate>
                                <asp:Label ID="lblCreateTime" runat="server" Text='<%# Eval("CreateTime") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="Size">
                            <ItemTemplate>
                                <asp:Label ID="lblSize" runat="server" Text='<%# Eval("Size") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>

    Code in aspx.cs:

       public partial class WebForm3 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string[] folderlist = Directory.GetDirectories(Server.MapPath("Upload"));
    
                List<vmFile> filelist = new List<vmFile>();
                foreach(var item in folderlist)
                {
                    vmFile file = new vmFile();
                    file.Name = Path.GetFileName(item);
                    file.Path = item;
                    file.CreateTime = File.GetLastWriteTime(item);
                    file.Size = GetDirectorySize(item);
                    filelist.Add(file);
                }
    
                gvDic.DataSource = filelist;
                gvDic.DataBind();
            }
    
            protected void gvDic_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
    // use a hiddenfield to store the file or folder path HiddenField hidpath = (HiddenField)e.Row.FindControl("hidPath"); if (hidpath != null) { string[] subfolderlist = Directory.GetDirectories(hidpath.Value); List<vmFile> filelist = new List<vmFile>(); foreach (var item in subfolderlist) { vmFile file = new vmFile(); file.Name = Path.GetFileName(item); file.Path = item; file.CreateTime = File.GetLastWriteTime(item); file.Size = GetDirectorySize(item); filelist.Add(file); } GridView gvChild = (GridView)e.Row.FindControl("gvSubDic"); if (gvChild != null) { gvChild.DataSource = filelist; gvChild.DataBind(); } } } } protected void gvSubDic_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HiddenField hidpath = (HiddenField)e.Row.FindControl("hidPath"); if (hidpath != null) { DirectoryInfo di = new DirectoryInfo(hidpath.Value); List<vmFile> filelist = new List<vmFile>(); //get files foreach (var item in di.GetFiles()) { vmFile file = new vmFile(); file.Name = item.Name; file.CreateTime = item.LastWriteTime; file.Size = Math.Round((double)item.Length / (double)(1024), 2).ToString() + " KB"; filelist.Add(file); } GridView gvChild = (GridView)e.Row.FindControl("gvFile"); if (gvChild != null) { gvChild.DataSource = filelist; gvChild.DataBind(); } } } } //get the directory size private string GetDirectorySize(string path) { DirectoryInfo info = new DirectoryInfo(path); long size = 0; foreach (string file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)) { size += new FileInfo(file).Length; } return Math.Round((double)size / (double)(1024), 2).ToString() + " KB"; } } //define a model, used to store the file or folder information public class vmFile { public string Name { get; set; } public DateTime CreateTime { get; set; } public string Size { get; set; } public string Path { get; set; } }

    The folder as below:

    The screenshot as below:

    Reference:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-iterate-through-a-directory-tree 

    If click on the file name I need the download of this file.

    If you want to download the file, you could use the LinkButton control display the file name. And refer to the following link to download file:

    https://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 24, 2017 5:24 AM