question

OSD-4642 avatar image
0 Votes"
OSD-4642 asked ·

Self-destructive VB Application

Hi,

Have VB application and here I have few buttons, one of them is "Exit" and I have following code:

 Me.Close( )

Me.Close ( ) exists from this app, is it possible to delete this file as well / kind of self destructive?

dotnet-visual-basic
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered ·

The reason for failure is you have spaces in the path, try the following which I ran and works.

 Imports System.IO
    
 Namespace My
     Partial Friend Class MyApplication
         Public Property DeleteOnClose() As Boolean
    
         Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
             If DeleteOnClose Then
                 Dim batchCommands As String = String.Empty
    
                 Dim exeFileName As String = Reflection.Assembly.GetExecutingAssembly().
                         CodeBase.Replace("file:///", String.Empty).
                         Replace("/", "\")
    
                 If exeFileName.Contains(" ") Then
                     exeFileName = $"""{exeFileName}"""
                 End If
    
                 batchCommands &= "@ECHO OFF" & vbLf
                 batchCommands &= "ping 127.0.0.1 > nul" & vbLf
                 batchCommands &= "echo j | del /F "
                 batchCommands &= exeFileName & vbLf
                 batchCommands &= "echo j | del deleteMe.bat"
    
                 File.WriteAllText("deleteMe.bat", batchCommands)
                 Process.Start("deleteMe.bat")
             Else
                 File.WriteAllText("Test.txt", "Hello")
             End If
         End Sub
     End Class
 End Namespace
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
1 Vote"
Castorix31 answered ·

You can use the old J. Richter method : A clever way to delete a program
(P/Invoke to convert C++ to VB)


· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ah, Jeffrey Richter...
The op clearly does not know him
This is by far the only and best solution

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered ·

A simple way to delete an app but keep in mind the user can resurrect the app from the Windows recycle bin. Depending on the project type will determine where to place the following code e.g. in the main form's closing event.

 Public Sub DeleteMe()
    
     Dim batchCommands As String = String.Empty
    
     Dim exeFileName As String = Reflection.Assembly.
             GetExecutingAssembly().CodeBase.Replace("file:///", String.Empty).
             Replace("/", "\")
    
     batchCommands &= "@ECHO OFF" & vbLf
     batchCommands &= "ping 127.0.0.1 > nul" & vbLf
     batchCommands &= "echo j | del /F "
     batchCommands &= exeFileName & vbLf
     batchCommands &= "echo j | del deleteMe.bat"
    
     File.WriteAllText("deleteMe.bat", batchCommands)
     Process.Start("deleteMe.bat")

 End Sub
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OSD-4642 avatar image
0 Votes"
OSD-4642 answered ·

Thanks karen. for reply. Is there a way to delete file permanently (without sending it to the Recycle Bin)?

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Actually I was wrong about DEL sending files to the recycle bin, using DEL from a batch file will not place the file in the recycle bin.

0 Votes 0 ·
OSD-4642 avatar image OSD-4642 karenpayneoregon ·

Placement of Code: In my case, I have Form1 with several buttons. One button (lets say button 5) have Me.Close ( ).
Should I place your code under that?
Project properties is set to close the application when starting form is close i.e. Form1.

0 Votes 0 ·

Use the well-known Richter method (a reference for more than 25 years..)
published in MSDN

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered ·

You can place the code I provided in ApplicationEvents.vb. For a .NET Core WinForm project the file is shown in Solution Explorer. Shown below the code will execute when the app closes.

72622-11111111111.png

 Imports System.IO
    
 Namespace My
     Partial Friend Class MyApplication
         Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
             Dim batchCommands As String = String.Empty
    
             Dim exeFileName As String = Reflection.Assembly.GetExecutingAssembly().
                     CodeBase.Replace("file:///", String.Empty).
                     Replace("/", "\")
    
             batchCommands &= "@ECHO OFF" & vbLf
             batchCommands &= "ping 127.0.0.1 > nul" & vbLf
             batchCommands &= "echo j | del /F "
             batchCommands &= exeFileName & vbLf
             batchCommands &= "echo j | del deleteMe.bat"
    
             File.WriteAllText("deleteMe.bat", batchCommands)
             Process.Start("deleteMe.bat")
    
         End Sub
     End Class
 End Namespace

Same can be done with earlier .NET Framework, under project properties.

72606-222222.png



11111111111.png (5.0 KiB)
222222.png (33.6 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, will give it a try.
Will this always delete the app? In my situation, I would like to to this only a specific button is pressed (let say button 1).

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered ·

In regards to delete on button click.

Revised code

 Imports System.IO
    
 Namespace My
     Partial Friend Class MyApplication
         Public Property DeleteOnClose() As Boolean
    
         Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
             If DeleteOnClose Then
                 Dim batchCommands As String = String.Empty
    
                 Dim exeFileName As String = Reflection.Assembly.GetExecutingAssembly().
                         CodeBase.Replace("file:///", String.Empty).
                         Replace("/", "\")
    
                 batchCommands &= "@ECHO OFF" & vbLf
                 batchCommands &= "ping 127.0.0.1 > nul" & vbLf
                 batchCommands &= "echo j | del /F "
                 batchCommands &= exeFileName & vbLf
                 batchCommands &= "echo j | del deleteMe.bat"
    
                 File.WriteAllText("deleteMe.bat", batchCommands)
                 Process.Start("deleteMe.bat")
             Else
                 File.WriteAllText("Test.txt", "Hello")
             End If
         End Sub
     End Class
 End Namespace

Button Click

 Public Class Form1
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         My.Application.DeleteOnClose = True
     End Sub
 End Class
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OSD-4642 avatar image
0 Votes"
OSD-4642 answered ·

Thanks again Karen. for follow up, appreciated.

I did the same as you described, however, in my case exe file wasn't deleted. I assume there might be something I am missing.
I have uploaded project file here: s!Amy6U9MHbS_NhArxuxuT8LOzvusu
Can you see what I am missing?

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OSD-4642 avatar image
0 Votes"
OSD-4642 answered ·

Thanks for sharing Karen.

I have commented out the text file creation as:
' File.WriteAllText("Test.txt", "Hello")
So far solution is still functional, do you see any advantages to have it anyways to retain the functionality?

The batch file executes and opens a command window, possible to have a hidden /minimized command window?

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't have time today to write up a perfect code sample but the idea is to use ProcessStartInfo as presented in the following GitHub Gist which I created for a C# article and did a conversion to VB.NET. Take a look at it. If I have time tomorrow I may have time to write up a merge between this and the code I have already presented. But don't wait for me, give it a shot.


0 Votes 0 ·
OSD-4642 avatar image OSD-4642 karenpayneoregon ·

Thanks Karen. for confirming that it is possible to hide the prompt.
Following Shell command worked for me.
Shell("deleteMe.bat", AppWinStyle.MinimizedNoFocus, False, 0)

0 Votes 0 ·