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