How do you open an embedded resource?

I am trying to make a program that will will make it easier for me to access some of my smaller frequently used program. I am new to Visual Basic and just discovered how to embed a .exe file into the project. I just dont know how to open it now. Basically there is a button and when it is clicked it needs to open up a .exe file, lets just call it 1.exe. I have looked around everywhere on how to do this and all I found was some resourcemanager commands but I have not been able to get them to work can someone please help me and post some sample code because like I said I just started using visual basic.
Thanks


Answer this question

How do you open an embedded resource?

  • Gheatza

    In the following example, I have temporarily borrowed

    Microsoft's calc.exe program and added it as an embedded

    resource. I then write it out as a new file named 'zzzz.exe' to

    show that I am not running the original 'calc.exe'.

    (Of course I could not redistribute this without MS's permission.

    You can alway launch a MS program that should exist, but you

    cannot legally embed it and redistribute it like this.)

    Private Sub Button2_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button2.Click

    Dim data() As Byte = My.Resources.calc

    Dim file As New System.IO.FileStream("zzzz.exe", IO.FileMode.Create)

    file.Write(data, 0, data.Length)

    file.Close()

    ' Run the Calc program named zzzz

    Dim p As New Process

    p.StartInfo.FileName = "zzzz.exe "

    p.Start()

    End Sub



  • th0

    Your code is a jumble of errors....

    This is what it would look like.




    Public Class Form1
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim Some_Stream As System.IO.Stream
            Some_Stream = GetManifestResourceStream("EXE1")
        End Sub

        Public Overridable Function GetManifestResourceStream(ByVal name As String) As System.IO.Stream
            Return Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(name)
        End Function
    End Class

     



    Dustin.


  • QuintPro

    I did that and it says alot of stuff. under SLysoft.My.Resources there is a resource folder that has my resources in it. one of them is "anydvd : byte" and there is a sub item under that which says "slysoft.My.Resources.Resources.get_anydvd() : Byte[]" Then outside of the Slysoft.MY.Resources folder there is another one that says Resources and has all of my resources in it also but they are all preceded bu slysoft (i.e. slysoft.anydvd.exe). I tried putting that into my code and it would not work. ANy ideas.

    Thanks

  • Simon Molloy

    Here is how you can write simplified VB 2005 code.  My solution is very similar to the approaches above.  I simplify things by using the Project Designer Resources tab and the My.Computer.FileSystem namespace. 

    Steps:
    - open the Project Designer by double-clicking on the My Project node in Solution Explorer (this is where you design the entire application)
    - click on the Resources tab (this is where you add and edit embedded resources)
    - drag and drop your program.exe file from any windows explorer window into the Resources window (I used the Mshearts.exe file)
    - add this code



        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Me.runApp(My.Resources.mshearts, "mshearts.exe")

        End Sub

        ''' <summary>
        ''' runs an embedded application resource
        ''' </summary>
        ''' <param name="appBinaryData">application binary data</param>
        ''' <param name="appName">name of the application with extension, e.g. "mshearts.exe"</param>
        ''' <remarks>pass in binary data from My.Resources</remarks>
        Public Sub runApp(ByVal appBinaryData As Byte(), ByVal appName As String)

            'TODO: add code validate inputs as appropriate

            'choose a file path to save to in the current program folder
            Dim appPath As String = My.Application.Info.DirectoryPath & "\" & appName

            'save the embedded app binary to the file path
            My.Computer.FileSystem.WriteAllBytes(appPath, My.Resources.mshearts, False)

            'start the process
            'warning -- validate users should be allowed to execute the value of appPath!
            Process.Start(appPath)

        End Sub

     

    Enjoy,

    Paul Yuknewicz


  • bcooke



     
    I took a look at it but still cannot get it to work I am using this

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


    Public Overridable Function GetManifestResourceStream ( _ name As < XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />String _ ) As Stream




    Dim instance As Assembly Dim name As String Dim returnValue As Stream returnValue = instance.GetManifestResourceStream(name)


    End Sub




    is this how I am supposed to do it. Like I said I just started using visual basic and
    there is ALOT that I do not know how to do.
    Thanks,

  • Gabriel_XD

    Okay. That tells me a little more. You can use My.Resources.anydvd to access the bytes of the executable. You will need to save these bytes off as a file using a FileStream and then run the executable using the Process class.



  • HY

    thanks but I already did that. It is listed as an embedded resource in my .resx file and in my project in the solution explorer. what is the name of the resource. If the filename was anydvd.exe wouldnt that be the resource name also or would it be something different Thanks for the help.
  • Mike Royle

    Ok. I think I got it.




    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Reflection;
    using System.IO;


    void useEmbeddedExecutable()

    {

       Assembly ens = Assembly.GetExecutingAssembly();

       //read file to byte array

       Stream s = ens.GetManifestResourceStream("anyproject.anyfile.exe");
       byte[] bytes = new byte[s.Length];
       s.Read(bytes, 0, (
    int) s.Length); 
       s.Close();

       //copy file

       FileStream sw = File.Create(@"C:\anyproject\anyfile.exe");
       sw.Write(bytes, 0, (
    int) bytes.Length); 
       sw.Close(); 

       //launch executable

       Process myProcess =
    new Process(); 
       myProcess.StartInfo.FileName = "C:\anyproject\anyfile.exe";
       myProcess.Start();   

    }



     



    Now make it VB...

    Cheers

    Mentoche.


  • Patricia Marchand

    I am in extreme need of this program. I used the code in the reply marked answer, but i got an error message saying that Process.Start(appPath) was an invalid parameter, how can i solve this HELP!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Michael Stokesbary

    It is quite hard to determine the correct name for a resource as it is based on the default namespace of the project and the folder the resource is contained under the project.

    An easy way to determine the name of your resource is use a tool such as the free .NET Reflector. Open your application with it and look under the Resources node to see the name of your resource.



  • Nathanael4499

    That went so far above my head I couldnt even see it, lol.. I am just starting to get into visual basic and I know nothing about filestreams and process classes, I have tried to look them up but MSDN's explanations dont really help me, is there anyway you could please give me a quick short code example of how I would do that. THe only reas I really need to learn how to do this is because I plan on making alot more programs like this, (if I can ever get this one working).. Thanks,


  • Jetcity73

    Yep sorry, Dustin's example is for reading a stream from assembly that's been embedded as a straight resource and not in a resx.

    Simply add the executable to your project, right-click it in Solution Explorer and select Properties. Then in the Properties windows, change Build Action to EmbeddedResource.

  • alex express

    Thanks alot dustin for the help, I understand it alot more know but still cannot get it to work, below is the code for the form. I included it as a resource in Slysoft.resx and I set its Build Action to Embedded Resource. The name of the file is anydvd.exe I get no errors when building it but when I click on the button nothing happens.  Do you see any problems with the code below (I also took you the .exe and it still didnt work)
    Thanks a bunch.
     



    Imports System.IO
    Imports System.Security.Cryptography

    Public Class Slysoft
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Public Overridable Function GetManifestResourceStream(ByVal name As String) As System.IO.Stream
    Return Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(name)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim
    anydvd As System.IO.Stream
    anydvd = GetManifestResourceStream(
    "anydvd.exe")
    End Sub

    Private
    Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim clonedvd As System.IO.Stream
    clonedvd= GetManifestResourceStream(
    "clonedvd.exe")
    End Sub
    End Class

     


  • Bob Boehler

    Have a look at the Assembly.GetManifestResourceStream method.

  • How do you open an embedded resource?