Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to embed files in VB.NET?

Status
Not open for further replies.

mattp14

Programmer
Apr 8, 2005
35
US
Say i'm writing a program that depends on a few other .exe and .dll files to work correctly. Instead of including those in the same directory as my app, i'd like to have my app "generate" those files. Thus making it more robust, portable, and secure.

Is this possible? I've played around with the resource manager to no avail already.

Any help would be appreciated.

Thank you!
 
Thanks for the reply... reading through there helped me out quite a bit... but i'm still stuck on 1 thing.

I can't seem to get the right combination of filestreams and what not, to ACTUALLY write the file back to disk.

You advised the requester in that thread to do it, but it was never shown how. I looked at all sorts of class types trying to figure out how to do this (FileWriter, StreamWriter, File, etc)... but the closest I came was the File.Create() method. This creates a file, but I can't get the resource info to write to it.

Any ideas? (I hope that made sense)

Thanks again!
 
Google is a developer's best friend. type in "file stream writer vb.net"

one of the links is: which goes over some good info on playing with streams. I can't write anything up from home, but if I get a chance on monday I'll try to throw something together. There have been a few questions on this, maybe it's time for a FAQ.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
Hey man... that article was PERFECT! Thank you VERY much!

For those who are interested... here's the code that ended up working.

Dim executingAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetEntryAssembly()
Dim myNamespace As String = executingAssembly.GetName().Name.ToString()

Dim stream As New FileStream("C:\newFile.exe", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim reader As New BinaryReader(executingAssembly.GetManifestResourceStream(myNamespace + ".embeddedFile.exe"))

Dim input As Byte() = reader.ReadBytes(reader.BaseStream.Length)

reader.Close()

If Not (input Is Nothing) Then
Dim writer As New BinaryWriter(stream)
writer.Write(input)
writer.Close()
stream.Close()
End If

MsgBox("Done!")
 
Ah yes... forgot to include that part :)

Although it's not actually needed, if everywhere you make a reference to any reader or stream class, you just add System.IO. to the name.

But who wants to do that? :)

Thanks again!
 
One embedded file is executable. Is there a way to execute it without having to extract it somewhere in the disk?


Tnx
 
bclt, check the process method, there may be a way. I don't have the IDE at home, so I can't say. But you would think that if you could get the exe into memory, you could run it.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
System.Diagnostics.Process.Start(executingAssembly.GetManifestResourceStream(myNamespace + ".embeddedFile.exe"))

Tried this but not working. Suggest something ?
 
I would also love to know how to do this. I've been trying to use the Process class, as suggested, but no luck so far. It just wont accept any kind of stream as input.

Anyone else have any thoughts?
 
One embedded file is executable. Is there a way to execute it without having to extract it somewhere in the disk?
Yes, you would just use a MemoryStream instead.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
But how would the process be started? As far as I have found the process class doesnt offer any methods that accept streams as parameters.
 
I haven't managed to to that yet .... :(
I can't even imagine how the code will look like
 
Can anyone help with some code about memoryStream. I mean how to load in memory one exe embedded file and run it?

Tnx!
 
I'm still trying to figure it out... nothing so far.

Like I said before I can get it into the memory stream from being embedded, but i can not for the life of me find a way to run it without first outputting to disk somewhere.

Any help would be greatly appreciated if someone has any ideas :)

Thanks
 
How do u put it in memory so i try it also, and who knows; may be lucky
 
Here is the code i've pieced together so far (some of it's my own, some isnt)

This code should read an embedded resource into the memory stream... it just needs to be launched!

Code:
imports System.IO
imports Microsoft.Win32

Public Sub runInMemory(ByVal fileName As String)
        Dim executingAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetEntryAssembly()
        Dim myNamespace As String = executingAssembly.GetName().Name.ToString()

        'Establish a stream to the embedded resource
        Dim reader As New BinaryReader(executingAssembly.GetManifestResourceStream(myNamespace & "." & fileName))

        'Read the embedded resource and close the reader
        Dim data As Byte() = reader.ReadBytes(reader.BaseStream.Length)
        reader.Close()

        'Load the resource into a memory stream
        Dim memStream As New MemoryStream(data)

        'Code for launching the process from memory goes here
        'Code for launching the process from memory goes here
        'Code for launching the process from memory goes here
    End Sub
 
You can load another assembly from a byte array with:
Code:
Dim A as [Assembly] = [Assembly].Load(ByteArrayHere)
So, if the exe is a .net assembly, you can call the public shared Main method this way, but it will run in your thread of execution. You might could get it to run in another AppDomain (almost like running in it's own process), but I don't have any experience with using them.

Here's an interesting idea for you -

.net has a way of creating assemblies dynamically via code you write. I fooled around with it a little bit. You use the System.Reflection.Emit namespace, which has a class called AssemblyBuilder. You use other classes (such as TypeBuilder to create classes, and MethodBuilder to create methods in the classes), and then call AssemblyBuilder.CreateInstance() to create an instance of it.

This way it all happens programatically, there's no embedded resource, and you can customize the code based on what your existing code needs it to do.

This is probably more than you wanted to do, but keep it in mind for a rainy day.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hmm. That's very interesting info Chip. Thanks. I'll definitely take a look at it. Although this may not work for my intended purpose... as I don't think the file i want to execute (in this particular program) was written in .NET


It definitely bears looking at though... would you have any idea how to launch a process from a memory stream?

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top