| 查看: 1284 | 回复: 2 | |||
| 本帖产生 1 个 程序强帖 ,点击这里进行查看 | |||
[交流]
VB.net实现删除文件的问题
|
|||
| 用VB.net实现删除同一文件夹下相同名称的所有文件,表达不好,还是举个例子吧,比如说删除某一文件夹下有zz.txt,zz.doc,zz.shp 。。。。就是名为zz的全删除,用VB.net编程该如何实现? |
» 猜你喜欢
国社科又开始会评了,不知道这次命运如何
已经有12人回复
麻烦专家们看看评委们的意见(F口面上)
已经有13人回复
售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急
已经有4人回复
售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急
已经有6人回复
为什么资助数各大高校都创新高,自己申请怎么就这么难
已经有15人回复
科研人应该花精力去思考如何解决问题,而不是去凝练问题
已经有14人回复
要骂人了,新模版改版就是要淡化问题凝练这种虚的东西,结果有个评委还在说凝练得不够
已经有17人回复
基金系统什么内容也没有
已经有10人回复
学科评审组评审是指会评吗?
已经有5人回复
面上函评意见出来了,像什么等级?
已经有17人回复
★ ★ ★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
微尘、梦想(金币+5, 程序强帖+1): 谢谢参与应助! 2011-05-14 19:50:35
小木虫(金币+0.5):给个红包,谢谢回帖
微尘、梦想(金币+5, 程序强帖+1): 谢谢参与应助! 2011-05-14 19:50:35
|
http://webservices.ctocio.com.cn/net/182/9255182.shtml VB.NET版 Imports System.IO Imports System.IO.Directory ' ====================================================== ' 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面 ' 如果目标文件夹为只读属性就会报错。 ' ====================================================== Public Shared Sub CopyDir(ByVal srcPath As String, ByVal aimPath As String) Try ' 检查目标目录是否以目录分割字符\结束,如果不是则添加之 If aimPath(aimPath.Length - 1) <> Path.DirectorySeparatorChar Then aimPath += Path.DirectorySeparatorChar End If '判断源目录是否存在,不存在则退出. If (Not Directory.Exists(srcPath)) Then Exit Sub ' 判断目标目录是否存在如果不存在则新建之 If (Not Directory.Exists(aimPath)) Then Directory.CreateDirectory(aimPath) ' 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组 ' 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法 ' string[] fileList = Directory.GetFiles(srcPath); Dim fileList() As String = Directory.GetFileSystemEntries(srcPath) ' 遍历所有的文件和目录 For Each FileName As String In fileList ' 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 If Directory.Exists(FileName) Then CopyDir(FileName, aimPath + Path.GetFileName(FileName)) ' 否则直接Copy文件 Else File.Copy(FileName, aimPath + Path.GetFileName(FileName), True) End If Next Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End Sub ' ====================================================== ' 实现一个静态方法将指定文件夹下面的所有内容Detele ' 测试的时候要小心*作,删除之后无法恢复。 ' ====================================================== Public Shared Sub DeleteDir(ByVal aimPath As String) Try ' 检查目标目录是否以目录分割字符结束如果不是则添加之 If (aimPath(aimPath.Length - 1) <> Path.DirectorySeparatorChar) Then aimPath += Path.DirectorySeparatorChar End If '判断待删除的目录是否存在,不存在则退出. If (Not Directory.Exists(aimPath)) Then Exit Sub ' 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组 ' 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法 ' string[] fileList = Directory.GetFiles(aimPath); Dim fileList() As String = Directory.GetFileSystemEntries(aimPath) ' 遍历所有的文件和目录 For Each FileName As String In fileList If (Directory.Exists(FileName)) Then ' 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件 DeleteDir(aimPath + Path.GetFileName(FileName)) Else ' 否则直接Delete文件 File.Delete(aimPath + Path.GetFileName(FileName)) End If Next '删除文件夹 System.IO.Directory.Delete(aimPath, True) Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End Sub |
2楼2011-05-14 16:41:07
★ ★ ★
微尘、梦想(金币+3): 谢谢! 2011-05-14 19:50:52
zzahkj(金币+10): 2011-05-15 23:18:29
微尘、梦想(金币+3): 谢谢! 2011-05-14 19:50:52
zzahkj(金币+10): 2011-05-15 23:18:29
|
用kill方法 Option Explicit Private Sub Command1_Click() Dim strPathName As String strPathName = "" strPathName = InputBox("请输入需要删除的文件夹名称∶", "删除文件夹" If strPathName = "" Then Exit Sub On Error GoTo ErrorHandle SetAttr strPathName, vbNormal '此行主要是为了检查文件夹名称的有效性 RecurseTree strPathName Label1.Caption = "文件夹" & strPathName & "已经删除!" Exit Sub ErrorHandle: MsgBox "无效的文件夹名称:" & strPathName End Sub Sub RecurseTree(CurrPath As String) Dim sFileName As String Dim newPath As String Dim sPath As String Static oldPath As String sPath = CurrPath & "\" sFileName = Dir(sPath, 31) '31的含义∶31=vbNormal+vbReadOnly+vbHidden+vbSystem+vbVolume+vbDirectory Do While sFileName <> "" If sFileName <> "." And sFileName <> ".." Then If GetAttr(sPath & sFileName) And vbDirectory Then '如果是目录和文件夹 newPath = sPath & sFileName RecurseTree newPath sFileName = Dir(sPath, 31) Else SetAttr sPath & sFileName, vbNormal Kill (sPath & sFileName) Label1.Caption = sPath & sFileName '显示删除过程 sFileName = Dir End If Else sFileName = Dir End If DoEvents Loop SetAttr CurrPath, vbNormal RmDir CurrPath Label1.Caption = CurrPath End Sub[ Last edited by jjdg on 2011-5-14 at 16:42 ] |
3楼2011-05-14 16:41:43










回复此楼
If strPathName = "" Then Exit Sub On Error GoTo ErrorHandle SetAttr strPathName, vbNormal '此行主要是为了检查文件夹名称的有效性 RecurseTree strPathName Label1.Caption = "文件夹" & strPathName & "已经删除!" Exit Sub ErrorHandle: MsgBox "无效的文件夹名称:" & strPathName End Sub Sub RecurseTree(CurrPath As String) Dim sFileName As String Dim newPath As String Dim sPath As String Static oldPath As String sPath = CurrPath & "\" sFileName = Dir(sPath, 31) '31的含义∶31=vbNormal+vbReadOnly+vbHidden+vbSystem+vbVolume+vbDirectory Do While sFileName <> "" If sFileName <> "." And sFileName <> ".." Then If GetAttr(sPath & sFileName) And vbDirectory Then '如果是目录和文件夹 newPath = sPath & sFileName RecurseTree newPath sFileName = Dir(sPath, 31) Else SetAttr sPath & sFileName, vbNormal Kill (sPath & sFileName) Label1.Caption = sPath & sFileName '显示删除过程 sFileName = Dir End If Else sFileName = Dir End If DoEvents Loop SetAttr CurrPath, vbNormal RmDir CurrPath Label1.Caption = CurrPath End Sub
30