VB中如何删除自身文件?

用VB编制的代码,生成工程(只有一个主文件,如test.exe)后,总共可以运行三次,当运行第三次后,退出时就把自已给删除了(不是删除到回收站,是彻底的删除)。求高手给个... 用VB编制的代码,生成工程(只有一个主文件,如test.exe)后,总共可以运行三次,当运行第三次后,退出时就把自已给删除了(不是删除到回收站,是彻底的删除)。求高手给个思路,最好能有代码。谢谢各位了,呵呵 展开
 我来答
匿名用户
2013-07-12
展开全部
首先,添加个资源文件,添加CUSTOM类型,编号101,弄个TXT写个0,加进去。然后在sub main 里,检测command,若不带参数就把自身读到二进制数组中,用CreateProcess函数从内存中执行程序并加上参数,然后END结束自身。这样做的目的是为了能在运行过程中修改自身CreateProcess函数可能会让杀毒软件报毒。当检测command发现带参数时,说明是从内存中启动的,这时候,loadresdata把101的数字读出来加上1,再用UpdataRes函数修改回去。最后,在form_unload 事件中,kill app.path & "\" & app.exename & ".exe"
匿名用户
2013-07-12
展开全部
模块:
Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Type SHFILEOPSTRUCT
hwnd As Long '窗口句柄
wFunc As Long '执行的操作
pFrom As String '原地点
pTo As String '目标地点
fFlags As Long '操作执行方式
fAnyOperationsAborted As Long '错误代码返回
hNameMappings As Long
lpszProgressTitle As String
End Type

Public Const FO_MOVE As Long = &H1
Public Const FO_COPY As Long = &H2
Public Const FO_DELETE As Long = &H3
Public Const FO_RENAME As Long = &H4

Public Const FOF_MULTIDESTFILES As Long = &H1
Public Const FOF_CONFIRMMOUSE As Long = &H2
Public Const FOF_SILENT As Long = &H4
Public Const FOF_RENAMEONCOLLISION As Long = &H8
Public Const FOF_NOCONFIRMATION As Long = &H10
Public Const FOF_WANTMAPPINGHANDLE As Long = &H20
Public Const FOF_CREATEPROGRESSDLG As Long = &H0
Public Const FOF_ALLOWUNDO As Long = &H40
Public Const FOF_FILESONLY As Long = &H80
Public Const FOF_SIMPLEPROGRESS As Long = &H100
Public Const FOF_NOCONFIRMMKDIR As Long = &H200

Form:
Private Sub Command1_Click()
Dim DelFileOp As SHFILEOPSTRUCT
Dim result As Long
With DelFileOp
.hwnd = Me.hwnd
.wFunc = FO_DELETE '(删除)
'.wfunc=fo_rename(改名) fo_move(移动) fo_copy(拷贝)
' Delete the files you just moved to C:\TestFolder.
' If you do not have these files, you can alter this
' sample to point to existing files.
' .pFrom = "C:\testfolder\file1" & vbNullChar & "c:\testfolder\file2" & vbNullChar & vbNullChar
.pFrom = "d:\testfolder\*" & vbNullChar & vbNullChar
'.pTo = "d:\test" (移动,拷贝时有效)
' Allow undo--in other words, place the files into the Recycle Bin
.fFlags = FOF_ALLOWUNDO
End With
result = SHFileOperation(DelFileOp)
If result <> 0 Then ' Operation failed
If Err.LastDllError <> 0 Then
MsgBox Err.LastDllError ' Msgbox the error that occurred in the API.
End If
Else
If DelFileOp.fAnyOperationsAborted <> 0 Then
MsgBox "Operation Failed"
End If
End If
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-12
展开全部
Private Sub Form_Unload(Cancel As Integer)
Shell "cmd /c del " & """" & App.Path & "\" & App.EXEName & ".exe" & """",vbhide
End Sub
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式