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,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,116 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. ~OSD~ 2,126 Reputation points
    2021-02-26T21:08:56.307+00:00

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


  2. ~OSD~ 2,126 Reputation points
    2021-03-02T09:04:40.42+00:00

    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?

    0 comments No comments

  3. ~OSD~ 2,126 Reputation points
    2021-03-02T23:48:49.15+00:00

    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?