string path = @"d:\desktop\aaa\"; // 目录aaa有bbb,目录bbb\还有 ccc,调用这个函数时提示目录不是空的,怎样才行、
Directory.Delete(path);
void DeleteDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
DirectoryInfo[] childs = dir.GetDirectories();
foreach (DirectoryInfo child in childs)
{
child.Delete(true);
} dir.Delete(true);
} MessageBox.Show("已经完全删除目录:" + path);
}
暂时没分啊。有分再加。
Directory.Delete(path);
void DeleteDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
DirectoryInfo[] childs = dir.GetDirectories();
foreach (DirectoryInfo child in childs)
{
child.Delete(true);
} dir.Delete(true);
} MessageBox.Show("已经完全删除目录:" + path);
}
暂时没分啊。有分再加。
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txt" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 112px" runat="server"
Width="544px" Height="416px" TextMode="MultiLine"></asp:TextBox>
<asp:Button id="btnCreate" style="Z-INDEX: 102; LEFT: 120px; POSITION: absolute; TOP: 80px"
runat="server" Text="Create directory"></asp:Button>
<asp:Button id="btnDelete" style="Z-INDEX: 103; LEFT: 272px; POSITION: absolute; TOP: 80px"
runat="server" Text="Delete directory"></asp:Button>
</form>.vb:Imports System.IOPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txt.Text = ""
End SubPrivate Sub CreateDir()
Dim i, j As Int16
Dim dirChildstr, dirSonstr As String If Directory.Exists(dirMain) = False Then
Directory.CreateDirectory(dirMain)
End If For i = 0 To 2
dirChildstr = dirMain & "\" & i + 1
If Directory.Exists(dirChildstr) = False Then
Directory.CreateDirectory(dirChildstr)
End If For j = 0 To 2
dirSonstr = dirChildstr & "\" & j + 1 If Directory.Exists(dirSonstr) = False Then
Directory.CreateDirectory(dirSonstr)
End If
Next
Next
End SubPrivate Sub DisplayDirList(ByVal strDir() As String) Dim i As Int16 For i = 0 To UBound(strDir)
txt.Text += strDir(i) + ControlChars.NewLine DisplayDirList(Directory.GetDirectories(strDir(i)))
NextEnd SubPrivate Sub DeleteDir(ByVal strDir() As String) Dim i, j As Int16 For i = 0 To UBound(strDir)
Dim dirSon(), fileSon() As String fileSon = Directory.GetFiles(strDir(i)) For j = 0 To UBound(fileSon)
File.Delete(fileSon(j))
Next dirSon = Directory.GetDirectories(strDir(i)) If dirSon.Length <> 0 Then
DeleteDir(dirSon)
End If Directory.Delete(strDir(i))
NextEnd SubPrivate ReadOnly Property dirMain() As String
Get
Return Server.MapPath("main")
End Get
End PropertyPrivate ReadOnly Property dirChild() As Array
Get
Return Directory.GetDirectories(dirMain)
End Get
End PropertyPrivate Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
CreateDir()
DisplayDirList(dirChild)
End SubPrivate Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
DeleteDir(dirChild)
DisplayDirList(dirChild)
End Sub