This example explains How To Delete Files Folders Directory And Sub Directories From Server In Asp.Net Using C# And VB.NET
I have placed one GridView on the aspx page to Display Directory or Contents of SubDirectory from server and have added one ButtonField In GridView Source for deletion when user clicks on button.
I have created one DataTable which is getting data with help of DirectoryInfo class.
I'm Deleting Files in RowCommand usingSystem.IO.File.Delete and Directories or foldersusing System.IO.Directory.Delete methods.
HTML SOURCE OF GRIDVIEW
C# CODE
VB.NET CODE
I have placed one GridView on the aspx page to Display Directory or Contents of SubDirectory from server and have added one ButtonField In GridView Source for deletion when user clicks on button.
I have created one DataTable which is getting data with help of DirectoryInfo class.
I'm Deleting Files in RowCommand usingSystem.IO.File.Delete and Directories or foldersusing System.IO.Directory.Delete methods.
HTML SOURCE OF GRIDVIEW
1: <asp:GridView ID="gridviewDeleteFiles" runat="server"
2: onrowcommand="gridviewDeleteFiles_RowCommand"
3: onrowdeleting="gridviewDeleteFiles_RowDeleting">
4: <Columns>
5: <asp:ButtonField Text="Delete" CommandName="Delete"/>
6: </Columns>
7: </asp:GridView>
C# CODE
01
using
System.Data;
02
using
System.IO;
03
protected
void
Page_Load(
object
sender, EventArgs e)
04
{
05
FillGridView();
06
}
07
private
void
FillGridView()
08
{
09
DataTable dtGridViewSource =
new
DataTable();
10
dtGridViewSource.Columns.Add(
new
DataColumn(
"Name"
,
typeof
(System.String)));
11
dtGridViewSource.Columns.Add(
new
DataColumn(
"Size"
,
typeof
(System.String)));
12
dtGridViewSource.Columns.Add(
new
DataColumn(
"Type"
,
typeof
(System.String)));
13
dtGridViewSource.Columns.Add(
new
DataColumn(
"Path"
,
typeof
(System.String)));
14
DataRow gridviewRow;
15
16
//Get All Folders Or Directories and add in table
17
DirectoryInfo directory =
new
DirectoryInfo(Server.MapPath(
"~/csharpdotnetfreak.blogspot.com"
));
18
DirectoryInfo[] subDirectories = directory.GetDirectories();
19
foreach
(DirectoryInfo dirInfo
in
subDirectories)
20
{
21
gridviewRow = dtGridViewSource.NewRow();
22
gridviewRow[
"Name"
] = dirInfo.Name;
23
gridviewRow[
"Type"
] =
"Directory"
;
24
gridviewRow[
"Path"
] = dirInfo.FullName;
25
dtGridViewSource.Rows.Add(gridviewRow);
26
}
27
//Get files in all directories
28
FileInfo[] files = directory.GetFiles(
"*.*"
, SearchOption.AllDirectories);
29
foreach
(FileInfo fileInfo
in
files)
30
{
31
gridviewRow = dtGridViewSource.NewRow();
32
gridviewRow[
"Name"
] = fileInfo.Name;
33
gridviewRow[
"Size"
] = fileInfo.Length;
34
gridviewRow[
"Type"
] =
"File"
;
35
gridviewRow[
"Path"
] = fileInfo.FullName;
36
dtGridViewSource.Rows.Add(gridviewRow);
37
}
38
gridviewDeleteFiles.DataSource = dtGridViewSource;
39
gridviewDeleteFiles.DataBind();
40
}
41
42
protected
void
gridviewDeleteFiles_RowCommand(
object
sender, GridViewCommandEventArgs e)
43
{
44
if
(e.CommandName ==
"Delete"
)
45
{
46
GridViewRow row = gridviewDeleteFiles.Rows[Convert.ToInt32(e.CommandArgument)];
47
string
fileName = row.Cells[1].Text;
48
string
filePath = row.Cells[4].Text;
49
string
type = row.Cells[3].Text;
50
if
(type ==
"File"
)
51
{
52
System.IO.File.Delete(filePath);
53
}
54
else
if
(type ==
"Directory"
)
55
{
56
System.IO.Directory.Delete(filePath,
true
);
57
58
}
59
}
60
FillGridView();
61
}
VB.NET CODE
01
Protected
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
02
FillGridView()
03
End
Sub
04
05
Private
Sub
FillGridView()
06
Dim
dtGridViewSource
As
New
DataTable()
07
dtGridViewSource.Columns.Add(
New
DataColumn(
"Name"
,
GetType
(System.
String
)))
08
dtGridViewSource.Columns.Add(
New
DataColumn(
"Size"
,
GetType
(System.
String
)))
09
dtGridViewSource.Columns.Add(
New
DataColumn(
"Type"
,
GetType
(System.
String
)))
10
dtGridViewSource.Columns.Add(
New
DataColumn(
"Path"
,
GetType
(System.
String
)))
11
Dim
gridviewRow
As
DataRow
12
13
'Get All Folders Or Directories and add in table
14
Dim
directory
As
New
DirectoryInfo(Server.MapPath(
"~/csharpdotnetfreak.blogspot.com"
))
15
Dim
subDirectories
As
DirectoryInfo() = directory.GetDirectories()
16
For
Each
dirInfo
As
DirectoryInfo
In
subDirectories
17
gridviewRow = dtGridViewSource.NewRow()
18
gridviewRow(
"Name"
) = dirInfo.Name
19
gridviewRow(
"Type"
) =
"Directory"
20
gridviewRow(
"Path"
) = dirInfo.FullName
21
dtGridViewSource.Rows.Add(gridviewRow)
22
Next
23
'Get files in all directories
24
Dim
files
As
FileInfo() = directory.GetFiles(
"*.*"
, SearchOption.AllDirectories)
25
For
Each
fileInfo
As
FileInfo
In
files
26
gridviewRow = dtGridViewSource.NewRow()
27
gridviewRow(
"Name"
) = fileInfo.Name
28
gridviewRow(
"Size"
) = fileInfo.Length
29
gridviewRow(
"Type"
) =
"File"
30
gridviewRow(
"Path"
) = fileInfo.FullName
31
dtGridViewSource.Rows.Add(gridviewRow)
32
Next
33
gridviewDeleteFiles.DataSource = dtGridViewSource
34
gridviewDeleteFiles.DataBind()
35
End
Sub
36
37
Protected
Sub
gridviewDeleteFiles_RowCommand(sender
As
Object
, e
As
GridViewCommandEventArgs)
38
If
e.CommandName =
"Delete"
Then
39
Dim
row
As
GridViewRow = gridviewDeleteFiles.Rows(Convert.ToInt32(e.CommandArgument))
40
Dim
fileName
As
String
= row.Cells(1).Text
41
Dim
filePath
As
String
= row.Cells(4).Text
42
Dim
type
As
String
= row.Cells(3).Text
43
If
type =
"File"
Then
44
System.IO.File.Delete(filePath)
45
ElseIf
type =
"Directory"
Then
46
47
System.IO.Directory.Delete(filePath,
True
)
48
End
If
49
End
If
50
FillGridView()
51
End
Sub
0 comments:
Post a Comment