Home > Back-end >  2 Diffrent bat files interfer with eachothers in vb.net code
2 Diffrent bat files interfer with eachothers in vb.net code

Time:02-15

I have a software that need to protect on offline machines. My problem is as the following 1-after some procedures to check whether the machine that running my app have a license or not, if not then the app delete itself by this code

Private Sub SelfDestruct()

     Dim procDestruct As Process = New Process()

     Dim strName As String = "destruct.bat"

     Dim strPath As String = Path.Combine _
      (Directory.GetCurrentDirectory(), strName)
     Dim strExe As String = New _
      FileInfo(Application.ExecutablePath).Name
     Dim swDestruct As StreamWriter = New StreamWriter(strPath)

     swDestruct.WriteLine("attrib """ & strExe & """" &
      " -a -s -r -h")
     swDestruct.WriteLine(":Repeat")
     swDestruct.WriteLine("del " & """" & strExe & """")
     swDestruct.WriteLine("if exist """ & strExe & """" &
      " goto Repeat")
     swDestruct.WriteLine("del """ & strName & """")

     swDestruct.Close()

     procDestruct.StartInfo.FileName = "destruct.bat"
     procDestruct.StartInfo.CreateNoWindow = True
     procDestruct.StartInfo.UseShellExecute = False

     Try

         procDestruct.Start()

     Catch ex As Exception

         Close()

     End Try

 End Sub

It worked just fine for 4 years until I found that I need to add another method to change Machine name and the only way the worked and actually changed the name is but doing it from a bat file too.(here come the conflict)

Private Sub PCRename()
        
     Dim Renameact As Process = New Process()
     Dim newname As String = "rename.bat"
     Dim strName As String = "rename.bat"

     Dim strPath As String = Path.Combine _
      (Directory.GetCurrentDirectory(), strName)
     Dim Renametext As StreamWriter = New StreamWriter(strPath)
     Dim rename As String = "wmic computersystem where name=""%computername%"" call rename name="
     Dim source As New System.Text.StringBuilder
     Renametext.WriteLine("@echo off")
     Renametext.WriteLine(rename   """"   newname    """")
     Renametext.WriteLine("shutdown /r -t 10")
     Renametext.WriteLine("del """ & strName & """")

     Renametext.Close()

     Renameact.StartInfo.FileName = "rename.bat"
     Renameact.StartInfo.CreateNoWindow = True
     Renameact.StartInfo.UseShellExecute = False
     MessageBox.Show("Computer will restart to complete the setup", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
     Try

         Renameact.Start()
         Application.ExitThread()
         Application.Exit()

     Catch ex As Exception

         Close()

     End Try
 End Sub

Now each time the name change the app delete itself even if I didn't call "SelfDestruct"

I tried the code but after comment the full code for "SelfDestruct" and the app didn't delete itself when I uncomment the "SelfDestruct" and test it the problem back again and the app still delete itself.

I googled every where for 2 days but with no luck.

is there a way to separate the methods or clean StreamWriter maybe(I tried Flush() & Dispose(), still didn't work).

Thank you.

CodePudding user response:

For Some reason this line is what cuase my app to get deleted

Renametext.WriteLine("del """ & strName & """")

when I delete it the app stay but also rename.bat file and I want to delete it so no one start the bat file by mistake.

CodePudding user response:

To solve this finally I delete the following line from the rename method

 Renametext.WriteLine("del """ & strName & """")

then add this line after calling the method

Exit Sub

Then used this code to delete the rename.bat file on the next launch

Dim Filenames As String = "rename.bat"
    Dim FilePath As String = "" & AppDomain.CurrentDomain.BaseDirectory & "" & Filenames & ""
    If System.IO.File.Exists(FilePath) = True Then
        System.IO.File.Delete(FilePath)
    End If
  • Related