Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted (edited)

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.

Edited by Cags
Anybody looking for a graduate programmer (Midlands, England)?
Posted
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.
  • Leaders
Posted

Use the following code to get the streams...

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()

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...