self destruct exe

Posted on 2006-05-02
Last Modified: 2008-01-09

I found the following code example which shows how to create a self-destruct exe file using c++.

Is it possible to do the same with .net?

Question by:narmi2
    LVL 85

    Accepted Solution

    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)

            Dim p As New Process
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            p.StartInfo.FileName = batFileFullPath
        End Sub
    LVL 1

    Author Comment

    That worked!

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

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    The batch file is created with these three lines:

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

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now