欢迎光临:
  
  非常感谢您光临枕善居。本站是一个免费的基于VB,VB.NET源代码交流的平台,为大家提供优质的专业的源代码,如果您有需要,本站可以帮助在业余时间里给您寻找代码。当然,如果您有好的代码也可以在本站发布,共享给大家。
专业VB和.NET源码、编程开发教程、图标资源、USB电脑遥控器、智能家电控制开关....更多东东请进入我的淘宝小店--->
VB及.NET新源码2011(3DVD,控件+资源) 智能多路控制(串口编程开关) 带源码!


05-07
03

安全删除自身

注意:这个代码不要在VB的IDE环境中执行.否则将删除VB6.EXE

Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    'Better than App.EXEName because it also  gets the extension
    '(It's possible to run a program with any extension, not just .exe)


Private Function FullAppName() As String
    Dim modName As String * 256
    Dim i As Long
    i = GetModuleFileName(App.hInstance, modName, Len(modName))
    FullAppName = Left$(modName, i)
End Function


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    'All these batch file commands
    'ensure that your program has
    'completely unloaded and
    'is not in use,
    'so your program can be
    'successfully deleted
    'Create a batch file to write to
    Open "a.bat" For Output As #1
    'This is pretty pointless but I
    'just want to put it here
    Print #1, "@ECHO OFF"
    'Create a label to mark the start
    'of the batch file
    Print #1, ":START"
    'If your program doesn't exist,
    'it goes to the FILENOTFOUND label
    Print #1, "IF NOT EXIST " & Chr(34) & FullAppName & Chr(34) & " GOTO FILENOTFOUND"
    'If the file is found, try to
    'delete it
    Print #1, "DEL " & Chr(34) & FullAppName & Chr(34)
    'Repeat the steps
    Print #1, "GOTO START"
    'If the file isn't found,
    'then that means your program
    'has been deleted
    Print #1, ":FILENOTFOUND"
    'Delete the batch file
    '(Batch files can delete
    'themselves while they're
    'still running)
    Print #1, "DEL A.BAT"
    'Clean everything up
    Print #1, "CLS"
    'Exit the batch file
    Print #1, "EXIT"
    'Close the file
    Close #1
    'Finally, execute the
    'batch file, since we only
    'wrote the commands, not ran
    'them
    Shell "a.bat", vbHide
    'Exit this program
    End
End Sub


相关日志:
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 8 | 引用: 0 | 查看次数: 7754

回复回复yyfearth [2006-07-11 19:00:41 |  | del]
现在我解决了不支持中文这个问题
这个函数用于获得自身文件名
因为自身文件名不一定是“.exe”文件,故以下代码优于App.EXEName + ".exe"

同时解决“不要在VB的IDE环境中执行.否则将删除VB6.EXE”的问题(最后一句)

Private Function FullAppName() As String
    Dim modName As String * 256
    Dim i As Long
    i = GetModuleFileName(App.hInstance, modName, Len(modName))
    i = InStr(1, modName, Chr(0)) - 1
    FullAppName = Left(modName, i)
    If LCase(Right(FullAppName, 7)) = "vb6.exe" Then MsgBox "请不要在VB的IDE环境中执行。", vbExclamation: End
End Function
回复回复yyfearth [2006-02-03 20:45:14 |  | del]
删不掉是因为部支持中文
原因是Len的问题,一个中文算2
只要全是半角字符(如 c:\asdf\asdf\project1.exe)就没问题
回复回复tian9246 [2005-07-23 11:46:57 |  | del]
我也是删除不了的
回复回复Jawin [2005-07-07 14:53:22 |  | del]
用批处理删除的。
如果一个程序有多个实体时(运行了多个),删不了。
del %0 可以删除自身(批处理文件)
回复回复论剑书生 [2005-07-03 17:51:50 |  | del]
呵呵,还有一……我不太好意思说了,似乎是用C实现的方法,我把全文ctrlC ctrlV过来吧~~~~~(坦白说,我看不太懂,也不知道在能不能在VB实现,可以先看一下)

http://www.mndsoft.com/bbs/showbbs.asp?topage=1&bd=11&id=85&totable=1
回复回复论剑书生 [2005-07-03 17:36:53 |  | del]
刚才去找了一下,发现有另外一种方法可以删除自身(不过需要重新启动)
----------------------------------------
Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Const MOVEFILE_DELAY_UNTIL_REBOOT = &H4
Private Function FullAppName() As String
    Dim modName As String * 256
    Dim i As Long
    i = GetModuleFileName(App.hInstance, modName, Len(modName))
    FullAppName = Left$(modName, i)
End Function

Private Sub Form_Unload(Cancel As Integer)
MoveFileEx FullAppName,"",MOVEFILE_DELAY_UNTIL_REBOOT
----------------------------------------
(呵呵,我没有试过——拨号上网,重新启动太麻烦了~~~[6])
有兴趣的可以试一下啊~~~
回复回复论剑书生 [2005-07-03 17:20:17 |  | del]
好像不支持中文名,程序也不能在中文名的文件夹中
回复回复lzanze [2005-07-03 16:50:25 |  | del]
删除 不了啊!!!

请你在仔细看看!

我编译成一个“工程1.exe”后

执行,然后关掉!文件还在啊!
发表评论
您没有权限发表评论!