This Example Show How To Display Files Directory Sub Directories In GridView From Server Folder In Asp.Net application using C# and Vb.Net.
We need to use DirectoryInfo class under System.IO namespace.
I'm displaying all files and directories (folders) from sever in gridview in page load.
I m creating a datatable in code behind with Name, Size and Type columns and assigning it as DataSource to Gridview.
Data in DataTable rows is coming from looping through all file and folder information stored in FileInfo or DirectoryInfo Arrays of GetFiles and GetDirectories methods of DirectoryInfo Class Object,
GetFiles method returns all the files and GetDirectories method returns all the directories or folder in server path specified.
To search all files i'm passing *.* and SearchOption.AllDirectories parameters in GetDirectories method,
we can display specific file type in gridview by specifying it's extension instead of *.* (for example *.jpg or filename.*). SearchOption.AllDirectories parameter search files in all sub directories
C# CODE
VB.NET
We need to use DirectoryInfo class under System.IO namespace.
I'm displaying all files and directories (folders) from sever in gridview in page load.
I m creating a datatable in code behind with Name, Size and Type columns and assigning it as DataSource to Gridview.
Data in DataTable rows is coming from looping through all file and folder information stored in FileInfo or DirectoryInfo Arrays of GetFiles and GetDirectories methods of DirectoryInfo Class Object,
GetFiles method returns all the files and GetDirectories method returns all the directories or folder in server path specified.
To search all files i'm passing *.* and SearchOption.AllDirectories parameters in GetDirectories method,
we can display specific file type in gridview by specifying it's extension instead of *.* (for example *.jpg or filename.*). SearchOption.AllDirectories parameter search files in all sub directories
C# CODE
protected void Page_Load(object sender, EventArgs e) { DataTable gvSource = DisplayFilesInGridView(); DataRow gvRow; //Get All Folders Or Directories and add in table DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com")); DirectoryInfo[] subDirectories = directory.GetDirectories(); foreach (DirectoryInfo dirInfo in subDirectories) { gvRow = gvSource.NewRow(); gvRow["Name"] = dirInfo.Name; gvRow["Type"] = "Directory"; gvSource.Rows.Add(gvRow); } //Get files in all directories FileInfo[] files = directory.GetFiles("*.*", SearchOption.AllDirectories); foreach (FileInfo fileInfo in files) { gvRow = gvSource.NewRow(); gvRow["Name"] = fileInfo.Name; gvRow["Size"] = fileInfo.Length; gvRow["Type"] = "File"; gvSource.Rows.Add(gvRow); } gridviewDisplayFilesDirectories.DataSource = gvSource; gridviewDisplayFilesDirectories.DataBind(); } private DataTable DisplayFilesInGridView() { DataTable dtGvSource = new DataTable(); dtGvSource.Columns.Add(new DataColumn("Name", typeof(System.String))); dtGvSource.Columns.Add(new DataColumn("Size", typeof(System.String))); dtGvSource.Columns.Add(new DataColumn("Type", typeof(System.String))); return dtGvSource; }
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs) Dim gvSource As DataTable = DisplayFilesInGridView() Dim gvRow As DataRow 'Get All Folders Or Directories and add in table Dim directory As New DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com")) Dim subDirectories As DirectoryInfo() = directory.GetDirectories() For Each dirInfo As DirectoryInfo In subDirectories gvRow = gvSource.NewRow() gvRow("Name") = dirInfo.Name gvRow("Type") = "Directory" gvSource.Rows.Add(gvRow) Next 'Get files in all directories Dim files As FileInfo() = directory.GetFiles("*.*", SearchOption.AllDirectories) For Each fileInfo As FileInfo In files gvRow = gvSource.NewRow() gvRow("Name") = fileInfo.Name gvRow("Size") = fileInfo.Length gvRow("Type") = "File" gvSource.Rows.Add(gvRow) Next gridviewDisplayFilesDirectories.DataSource = gvSource gridviewDisplayFilesDirectories.DataBind() End Sub Private Function DisplayFilesInGridView() As DataTable Dim dtGvSource As New DataTable() dtGvSource.Columns.Add(New DataColumn("Name", GetType(System.String))) dtGvSource.Columns.Add(New DataColumn("Size", GetType(System.String))) dtGvSource.Columns.Add(New DataColumn("Type", GetType(System.String))) Return dtGvSource End Function
If you like this post than join us or share
2 comments:
This comment has been removed by a blog administrator.
Useful post... Thanks...
Post a Comment