Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1077
  • Last Modified:

self destruct exe

Hi

I found the following code example which shows how to create a self-destruct exe file using c++.
http://www.codeproject.com/file/cpselfdestruct2.asp

Is it possible to do the same with .net?

Thanks
0
narmi2
Asked:
narmi2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is some equivalent code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tempPath As String = System.IO.Path.GetTempPath
        If Not tempPath.EndsWith("\") Then
            tempPath = tempPath & "\"
        End If
        Dim batFileName = "_uninsep.bat"
        Dim batFileFullPath As String = tempPath & batFileName
        Dim executable As String = Application.ExecutablePath

        Dim batFile As String = _
            ":Repeat" & vbCrLf & _
            "del ""{executable}""" & vbCrLf & _
            "if exist ""{executable}"" goto Repeat" & vbCrLf & _
            "del {batFileFullPath}" & vbCrLf
        batFile = batFile.Replace("{executable}", executable)
        batFile = batFile.Replace("{batFileFullPath}", batFileFullPath)

        Dim sw As New System.IO.StreamWriter(batFileFullPath, False)
        sw.Write(batFile)
        sw.Close()

        Dim p As New Process
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        p.StartInfo.FileName = batFileFullPath
        p.Start()
        Application.Exit()
    End Sub
0
 
narmi2Author Commented:
That worked!

But how?  Where does it create the bat file? and does it delete the bat file also?

Thanks!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The batch file is created with these three lines:

        Dim sw As New System.IO.StreamWriter(batFileFullPath, False)
        sw.Write(batFile)
        sw.Close()

The bat file deletes itself with the last line of the batch file:

        "del {batFileFullPath}" & vbCrLf

The {batFileFullPath} gets replaced with the full path to the bat file via the calls to Replace() command.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now