Self-destructive VB Application

~OSD~ 2,126 Reputation points
2021-02-26T12:23:21.237+00:00

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?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2021-03-02T20:44:53.753+00:00

    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
    
    1 person found this answer helpful.
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Castorix31 81,721 Reputation points
    2021-02-26T12:53:07.257+00:00

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

    1 person found this answer helpful.

  2. Karen Payne MVP 35,036 Reputation points
    2021-02-26T18:35:19.507+00:00

    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
    
    1 person found this answer helpful.
    0 comments No comments

  3. Karen Payne MVP 35,036 Reputation points
    2021-02-27T12:21:28.613+00:00

    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

    1 person found this answer helpful.

  4. Karen Payne MVP 35,036 Reputation points
    2021-03-01T12:15:39.337+00:00

    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
    
    1 person found this answer helpful.
    0 comments No comments