help: in including files in exe

tiger03

Member
Joined
Jun 3, 2006
Messages
7
Programming Experience
Beginner
Hi, First of all, I will describe my scenario. I have to do an update or a patch. So what I did was copied the exe files and the dll files into a folder and I have the exe that will execute in order to copy or overwrite the exising files in the c:\program files\my software folder.

So I was wondering if I could create a vb.net console project to give me an exe file which would include the dll and exe files so there would only be one exe file to send to the client and not the whole lot. I hope that makes sense. cheers!
 
You don't use a console app. That's what installers are for. You add a setup project to your to your solution and then build that into an MSI package. If you want to distribute the .NET Framework with your app then you'll still need to distribute more than one file. An alternative to a proper installer would be to just use something like WinZip to create a self-extracting archive.
 
Thanks for the reply, I have already got a set up project. I was wanting to do an upgrade as a patch. Found a way to do that by including files as Embedded Resource files and using manifestourstream to get them. Cheers.
 
Maybe I wasnt clear in the first place. My apologies. I still have one question, how do I use the file embedded file within the resource? For example its easy just to do something like File.copy() to a destination path but in this case I get returned a stream, I am not clear as to how to use this stream... This is my code:
VB.NET:
Dim cr As Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim files() As String = cr.GetManifestResourceNames() 
Dim stream As System.IO.Stream = cr.GetManifestResourceStream(files.GetValue(0))
Dim fs As FileStream = cr.GetFile("ConsoleApplication1.fileName.exe")
So what I would like to do is to be able to copy and paste a file literally. At the moment I am not sure if I can use the stream I get to do that.
update: I have managed to write something but its somewhat not working. This is what I am doing:
VB.NET:
Dim text_stream As Stream = cr.GetManifestResourceStream(files.GetValue(0))
Dim sw AsNew StreamWriter("mypath")
IfNot (text_stream IsNothing) Then
Dim stream_reader AsNew StreamReader(text_stream)
sw.Write(stream_reader.ReadToEnd())
stream_reader.Close()
sw.Close()
EndIf
so the resultant file is missing a few kbs and therefore not the actual file in it self.
 
Last edited:
The Stream you get from the resource is, like any stream, a series of bytes. If you want to save that to disc then you'll need to read the bytes from the resource stream and write them to another stream that is disc-based, like a FileStream.
 
A code sample for tiger03:
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] embeddedresourcetofile()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] executing_assembly [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Reflection.Assembly = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].GetType.Assembly.GetEntryAssembly()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] my_namespace [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = executing_assembly.GetName().Name.ToString()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fs [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.FileStream(Application.StartupPath & "\fileName.exe", IO.FileMode.Create)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IO.Stream = executing_assembly.GetManifestResourceStream(my_namespace & ".fileName.exe")
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] mem.Length - 1
fs.WriteByte(mem.ReadByte)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]fs.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

 
Thanks for that John, yeah I sorta had the idea of using byte to byte writing but couldnt get it across. Can you please explain to me why my code before was missing a few kb's? It seemed to be writing, and therefore the end file will not execute or open...

Your code has done the trick. Thanks alot!
 
You don't use a StreamWriter to to write binary data. StreamWriters are for text so it will try to interpret everything as text, which is probably not appropriate. Also, it would be more efficient to write blocks of bytes at a time rather than single bytes. That would incur a lot of overhead.
 
Back
Top