| 查看: 1308 | 回复: 2 | |||
| 本帖产生 1 个 程序强帖 ,点击这里进行查看 | |||
| 当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖 | |||
[交流]
VB.net实现删除文件的问题
|
|||
| 用VB.net实现删除同一文件夹下相同名称的所有文件,表达不好,还是举个例子吧,比如说删除某一文件夹下有zz.txt,zz.doc,zz.shp 。。。。就是名为zz的全删除,用VB.net编程该如何实现? |
» 猜你喜欢
申请科研项目纯靠实力能成功吗?
已经有9人回复
40+中年人失业后,还能找到好工作吗?
已经有4人回复
极限拉扯,如何抉择
已经有15人回复
中科院海洋所招收海洋材料相关领域博士研究生及研究助理
已经有4人回复
二审4个多月了还是Awaiting Reviewer Scores
已经有4人回复
OpenAI又攻克数学难题了,离AI做有机合成还有多远?
已经有13人回复
课题组会帮你通过考核么?
已经有7人回复
入职后第一次管课题组的专利缴费,对账才发现官费比别人多掏了一倍
已经有6人回复
售一区SCI文章T0P,我:8O.551.O54,科目全,可十急
已经有9人回复
售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急
已经有8人回复
★ ★ ★
微尘、梦想(金币+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
★ ★ ★ ★ ★ ★
小木虫(金币+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










回复此楼
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
200