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
01
protected
void
Page_Load(
object
sender, EventArgs e)
02
{
03
DataTable gvSource = DisplayFilesInGridView();
04
DataRow gvRow;
05
06
//Get All Folders Or Directories and add in table
07
DirectoryInfo directory =
new
DirectoryInfo(Server.MapPath(
"~/csharpdotnetfreak.blogspot.com"
));
08
DirectoryInfo[] subDirectories = directory.GetDirectories();
09
foreach
(DirectoryInfo dirInfo
in
subDirectories)
10
{
11
gvRow = gvSource.NewRow();
12
gvRow[
"Name"
] = dirInfo.Name;
13
gvRow[
"Type"
] =
"Directory"
;
14
gvSource.Rows.Add(gvRow);
15
}
16
//Get files in all directories
17
FileInfo[] files = directory.GetFiles(
"*.*"
, SearchOption.AllDirectories);
18
foreach
(FileInfo fileInfo
in
files)
19
{
20
gvRow = gvSource.NewRow();
21
gvRow[
"Name"
] = fileInfo.Name;
22
gvRow[
"Size"
] = fileInfo.Length;
23
gvRow[
"Type"
] =
"File"
;
24
gvSource.Rows.Add(gvRow);
25
26
}
27
gridviewDisplayFilesDirectories.DataSource = gvSource;
28
gridviewDisplayFilesDirectories.DataBind();
29
}
30
31
private
DataTable DisplayFilesInGridView()
32
{
33
DataTable dtGvSource =
new
DataTable();
34
dtGvSource.Columns.Add(
new
DataColumn(
"Name"
,
typeof
(System.String)));
35
dtGvSource.Columns.Add(
new
DataColumn(
"Size"
,
typeof
(System.String)));
36
dtGvSource.Columns.Add(
new
DataColumn(
"Type"
,
typeof
(System.String)));
37
return
dtGvSource;
38
}
VB.NET
01
Protected
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
02
Dim
gvSource
As
DataTable = DisplayFilesInGridView()
03
Dim
gvRow
As
DataRow
04
05
'Get All Folders Or Directories and add in table
06
Dim
directory
As
New
DirectoryInfo(Server.MapPath(
"~/csharpdotnetfreak.blogspot.com"
))
07
Dim
subDirectories
As
DirectoryInfo() = directory.GetDirectories()
08
For
Each
dirInfo
As
DirectoryInfo
In
subDirectories
09
gvRow = gvSource.NewRow()
10
gvRow(
"Name"
) = dirInfo.Name
11
gvRow(
"Type"
) =
"Directory"
12
gvSource.Rows.Add(gvRow)
13
Next
14
'Get files in all directories
15
Dim
files
As
FileInfo() = directory.GetFiles(
"*.*"
, SearchOption.AllDirectories)
16
For
Each
fileInfo
As
FileInfo
In
files
17
gvRow = gvSource.NewRow()
18
gvRow(
"Name"
) = fileInfo.Name
19
gvRow(
"Size"
) = fileInfo.Length
20
gvRow(
"Type"
) =
"File"
21
22
gvSource.Rows.Add(gvRow)
23
Next
24
gridviewDisplayFilesDirectories.DataSource = gvSource
25
gridviewDisplayFilesDirectories.DataBind()
26
End
Sub
27
28
Private
Function
DisplayFilesInGridView()
As
DataTable
29
Dim
dtGvSource
As
New
DataTable()
30
dtGvSource.Columns.Add(
New
DataColumn(
"Name"
,
GetType
(System.
String
)))
31
dtGvSource.Columns.Add(
New
DataColumn(
"Size"
,
GetType
(System.
String
)))
32
dtGvSource.Columns.Add(
New
DataColumn(
"Type"
,
GetType
(System.
String
)))
33
Return
dtGvSource
34
End
Function
0 comments:
Post a Comment