问题描述:
请问VB编写删除电脑里某个文件用什么代码?要用到哪个API???给一个例子,要注释的详细些?我初学VB,我看懂了给30分!!!
解析:
你可以看看kill函数:
kill从磁盘中删除文件。
Kill pathname
pathname 参数是用来指定一个文件名的字符串表达式。
pathname 可以包含目录或文件夹、以及驱动器。
说明
在 Microsoft Windows 中,Kill 支持多字符 (*) 和单字符 (?) 的通配符来指定多重文件。
例子:
Private Sub Command1_Click()
Kill "C:\aa.txt" '文件的绝对路径"
End Sub
如果是把文件彻底删除,楼上都是对的!
但是如果你是把文件放到“回收站”之中,而不是彻底的删除就要用到vb的
api函数了!
例子:
在公共模块之中:
Option Explicit
Public Const FO_MOVE = &H1
Public Const FO_COPY = &H2
Public Const FO_DELETE = &H3
Public Const FO_RENAME = &H4
Public Const FOF_NOCONFIRMATION = &H10
Public Const FOF_NOCONFIRMMKDIR = &H200
Public Const FOF_ALLOWUNDO = &H40
Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
在frm模块之中:
Private Sub Command1_Click()
Dim SHFileOp As SHFILEOPSTRUCT
Dim Path As String, pFrom As String, i As Integer
pFrom ="c:\aa.txt"
SHFileOp.wFunc = FO_DELETE
SHFileOp.pFrom = pFrom
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
SHFileOperation SHFileOp
End Sub
我想楼主应该是将文件放入”回收站“的意思,所以。。。
Private Sub Command1_Click()For i = 0 To List1.ListCount - 1
Kill App.Path &"\" &List1.List(i)
Next
List1.Clear
End Sub
Private Sub Form_Load()
a = Dir(App.Path &"\", vbHidden + vbDirectory)
Do While a <>""
If GetAttr(App.Path &"\" &a) <>vbDirectory Then
If Right(a, 3) = "txt" Then List1.AddItem a
End If
a = Dir
Loop
End Sub
你罗列的这几个函数API,在其后面把路径带进去调用就可以了 已经是很简单了。还要说明什么?Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Sub Form_Load()
DeleteFile "c:\test.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
fs.DeleteFile "c:\test.txt"
fs.deletefolder "c:\test"
Kill "c:\test.txt"
RmDir "c:\test"
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)