Results 1 to 12 of 12

Thread: Embedded Resource

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    3

    Embedded Resource

    Hi!
    I have embedded an .exe file in my project... How can I in the most easy way save this file on the HD?

    Thanks /Ale

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Wink Use the Assembly

    Hi.

    You can use the reflection.assembly for this.

    Code:
    Dim A As Reflection.Assembly = reflection.Assembly.GetExecutingAssembly
    
    Dim ST As IO.Stream = A.GetManifestResourceStream("AppName.OrginalFilename.Exe")
    
    Dim B(ST.Length - 1) As Byte
    
    ST.Read(B, 0, B.Length)
    
    Dim ST2 As IO.Stream = IO.File.OpenWrite("NewFileName.exe")
    ST2.Write(B, 0, B.Length)
    
    ST.Close()
    ST2.Close()
    I hope this help.

    Cheers
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    3
    Hi, really thanks for your help... But I can't get it to work...

    Dim B(ST.Length - 1) As Byte

    This line says something like: objectreferense has not been choosen to an instance of an object

    What to do, any ideas?

    /Ale

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Make sure you type the correct file name !

  5. #5
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    How were you able to embed the exe file? Can you share some code?

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    3
    I'm really sure I have the right filename, it doesn't work anyway...

    How to embedd your exe: Right click your project in the solution window, then add existing item, choose all files and select your exe... The change from "compile" to "embedded resoure" in the properties window... Hopes this helps...

    /Ale

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Can you show us how you tried the above code ?

  8. #8
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Exclamation

    I have the same problem with your code,

    I have just put this code behind a button:

    Code:
    Dim A As Reflection.Assembly = reflection.Assembly.GetExecutingAssembly
    
    Dim ST As IO.Stream = A.GetManifestResourceStream("AppName.OrginalFilename.Exe")
    
    Dim B(ST.Length - 1) As Byte
    
    ST.Read(B, 0, B.Length)
    
    Dim ST2 As IO.Stream = IO.File.OpenWrite("NewFileName.exe")
    ST2.Write(B, 0, B.Length)
    
    ST.Close()
    ST2.Close()
    and this is the error message:


    Code:
    An unhandled exception of type 'System.NullReferenceException' occurred in ModelInstaller.exe
    §tudz

    Studzworld.com - Portfolio

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    the embedded file must be spot on when trying to read it, this is a quick sample which works fine with an embedded file...
    VB Code:
    1. [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Button1_Click([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] System.Object, [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] Button1.Click
    2.         [COLOR=BLUE]Dim[/COLOR] path [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR] = Application.ProductName & ".closing forms.exe" [COLOR=GREEN]'/// must be the exact path of the embedded exe.
    3. [/COLOR]        [COLOR=BLUE]Dim[/COLOR] sReader [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader([COLOR=BLUE]MyBase[/COLOR].GetType.Assembly.GetManifestResourceStream(path))
    4.         [COLOR=BLUE]Dim[/COLOR] sWriter [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamWriter([COLOR=BLUE]New[/COLOR] IO.FileStream("D:\closing_forms.exe", IO.FileMode.OpenOrCreate))
    5.         [COLOR=BLUE]While[/COLOR] [COLOR=BLUE]Not[/COLOR] sReader.Peek
    6.             sWriter.WriteLine(sReader.ReadLine)
    7.         [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]While
    8. [/COLOR]        sWriter.Close()
    9.         sReader.Close()
    10.     [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  10. #10
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    yeah, BUT I want it to save the file to the Hard Drive says for your example's sake:

    C:\test.txt


    Thank you,
    §tudz

    Studzworld.com - Portfolio

  11. #11
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    yeah, BUT I want it to save the file to the Hard Drive says for your example's sake:

    C:\test.txt
    this line saves the file to the specified hard disk path....
    VB Code:
    1. [color=blue]Dim[/color] sWriter [color=blue]As New[/color] IO.StreamWriter(New IO.FileStream("D:\closing_forms.exe", IO.FileMode.OpenOrCreate))
    where D:\ is the hard drive path i'm saving the .exe " closing_forms.exe" to.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  12. #12
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    I'm getting an error with your code too,

    this line

    Code:
    sReader As New IO.StreamReader(MyBase.GetType.Assembly.GetManifestResourceStream(path))
    produces an error says that it can not be Null,

    here is my Code:

    Code:
    Private Sub cmd_Install_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Install.Click
            Dim path As String = Application.ProductName & ".exe text.txt" '/// must be the exact path of the embedded exe.
            MessageBox.Show(path)
            Dim sReader As New IO.StreamReader(MyBase.GetType.Assembly.GetManifestResourceStream(path))
    
            Dim sWriter As New IO.StreamWriter(New IO.FileStream("C:\test.txt", IO.FileMode.OpenOrCreate))
            While Not sReader.Peek
                sWriter.WriteLine(sReader.ReadLine)
            End While
            sWriter.Close()
            sReader.Close()
    
    
        End Sub
    can you help point out where I am going wrong?

    thank you
    §tudz

    Studzworld.com - Portfolio

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width