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
Printable View
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
Hi.
You can use the reflection.assembly for this.
I hope this help.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()
Cheers
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
Make sure you type the correct file name !
How were you able to embed the exe file? Can you share some code?
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
Can you show us how you tried the above code ?
I have the same problem with your code,
I have just put this code behind a button:
and this is the error message: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()
Code:An unhandled exception of type 'System.NullReferenceException' occurred in ModelInstaller.exe
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:
[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 [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. [/COLOR] [COLOR=BLUE]Dim[/COLOR] sReader [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]New[/COLOR] IO.StreamReader([COLOR=BLUE]MyBase[/COLOR].GetType.Assembly.GetManifestResourceStream(path)) [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)) [COLOR=BLUE]While[/COLOR] [COLOR=BLUE]Not[/COLOR] sReader.Peek sWriter.WriteLine(sReader.ReadLine) [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]While [/COLOR] sWriter.Close() sReader.Close() [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub[/COLOR]
yeah, BUT I want it to save the file to the Hard Drive says for your example's sake:
C:\test.txt
Thank you,
this line saves the file to the specified hard disk path....Quote:
yeah, BUT I want it to save the file to the Hard Drive says for your example's sake:
C:\test.txt
where D:\ is the hard drive path i'm saving the .exe " closing_forms.exe" to.VB Code:
[color=blue]Dim[/color] sWriter [color=blue]As New[/color] IO.StreamWriter(New IO.FileStream("D:\closing_forms.exe", IO.FileMode.OpenOrCreate))
I'm getting an error with your code too,
this line
produces an error says that it can not be Null,Code:sReader As New IO.StreamReader(MyBase.GetType.Assembly.GetManifestResourceStream(path))
here is my Code:
can you help point out where I am going wrong?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
thank you