Compiling files into your exe - and then accessing them @ run time

Superfly1611

Regular
Joined
Feb 17, 2004
Messages
66
Location
UK
Hello all,

Out of nowhere i've been asked to write a small exe to distribute to our clients that will install some template files onto there computers. (Yes i've raised the issue of client's willingness to run foreign exe's on there computers).

Now back in my uni days i remember hearing someone mention that you could compile a file(s) into an exe and then access them @ run time.

Now I assume(???) that when compiling in .NET you set the Build Action to embedded resource to compile a file into your exe... but how do you then access this file?

Many Thanks
 
Do you wish to simply access them to copy them into a location on the end users PC? If this is the case then (I'm kinda guessing here I've not tested it), I'm pretty sure creating a Setup / Deployment project might allow you todo this. It certainly allows you to copy files to specified folders on the clients pc, quite how well it works when your not actually installing an application I couldn't say, but I think thats the way I'd attempt to do it.

EDIT:- I gave it a quick test and it did what I wanted, also has the added advantage of adding an item in the add/remove dialog. This would allow an end user to remove the templates.
 
Last edited:
I've done this before, basically you can add the file you want as a resource file into the solution, you gotta set it to compile in the exe, when needed, you can open the file as a temporary stream and write it to disk, as if it's copied, i'd advise you to keep all these templates as a class library so that way when u need to update them, you just update that particular dll.
 
Use the following code to get the streams...
Visual Basic:
Imports System.Reflection
'...
 
Dim Assm As Assembly = Assembly.GetExecutingAssembly()
Dim Stream As IO.Stream = _
Assm.GetManifestResourceStream("RootNamespace.Filename.withextension")
'here you can read from the stream and output the data to a filestream.
Stream.Close()
C#:
using System.Reflection;
// ...
 
Assembly Assm = Assembly.GetExecutingAssembly();
IO.Stream Stream = _
Assm.GetManifestResourceStream("RootNamespace.folderpath.Filename.withextension");
/* The folder path should be delimited with dots, not backslashes.
   Folder path is relative to the project's root folder. If the resource 
   file is in the project's root folder, omit the folder path. */
// here you can read from the stream and output the data to a filestream.
Stream.Close();
This is how it is done in .Net 1.0/1.1. As you can see, the resource naming conventions vary between C# and VB. I'm not sure that the naming convention has changed in 2.0, but 2.0 also has better tools for embedding resources, so you might want to click Help and do some MSDN research.
 
Thanks for u're help guys.
Haven't had a chance to try this as i'm now being dragged onto another project... but i'm sure your help is all good :)
 
Back
Top