views:

2658

answers:

5

Alright, so I'm working on programming my own installer in C#, and what I'd like to do is something along the lines of put the files in the .exe, so I can do File.Copy(file, filedir);

Or, if this isn't possible, is there another way of doing what I am attempting to do?

Thank you in advance.

~Seth

+3  A: 

I'm guessing here, but if you are trying to store resources in your application before compilation, you can in the Project Explorer, right click a file you would like to add, chose properties and change the type to Embedded Resource.

You can then access the embedded resources later by using the instructions from this KB: http://support.microsoft.com/kb/319292

Ray Booysen
+8  A: 

Honestly, I would suggest you NOT create your own installer. There are many many issues with creating installers. Even the big installer makers don't make their own actual installers anymore, they just create custom MSI packages.

Use Mirosoft Installer (MSI). It's the right thing to do. Make your own custom front-end for it, but don't recreate the already very complex wheel that exists.

UPDATE: If you're just doing this for learning, then I would shy away from thinking of it as "an installer". You might be tempted to take your "research" and use it someday, and frankly, that's how we end up with so many problems when new versions of Windows come out. People create their own wheels with assumptions that aren't valid.

What you're really trying to do is called "packaging", and you really have to become intimately familiar with the Executable PE format, because you're talking about changing the structure of the PE image on disk.

You can simulate it, to a point, with putting files in resources, but that's not really what installers, or self-extractors do.

Here's a link to Self-Extractor tutorial, but it's not in C#.

I don't know enough about the .NET PE requirements to know if you can do this in with a managed code executable or not.

UPDATE2: This is probably more of what you're looking for, it embeds files in the resource, but as I said, it's not really the way professional installers or self-extractors do it. I think there are various limitations on what you can embed as resources. But here's the like to a Self-Extractor Demo written in C#.

Mystere Man
I would definitely research all the available installers out there BEFORE writing my own. This is sort of what I was talking about in "Don't Reinvent The Wheel, Unless You Plan on Learning More About Wheels" http://www.codinghorror.com/blog/archives/001145.html
Jeff Atwood
The only reason I was, was to see if I could.I'm not trying to make it adv or anything, just trying to create 1 form that when you click Install and it just puts the files in a directory.
S3THST4
S3thst4,Definitely do the research on available installers, but don't let the nay-sayers put you off, most available installers leave much to be desired especially MSI, there definitely is room for massive improvement, installers are no walk in the park though, be prepared for hard graft.
Tim Jarvis
+2  A: 

I wouldn't code my own installer, but if you truely want to embed files into your assembly you could use strongly typed resources. In the properties dialog of your project open up the "Resources" tab and then add your file. You'll then be able to get the file using:

ProjectNamespace.Properties.Resources.MyFile

Then you'll be able to write the embedded resource to disk using:

System.IO.File.WriteAllBytes(@"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);
Shawn Miller
Thank you for your response, this is what I am looking for.Alright, so I'm using File.Copy(); and I need a string overload for the Filepath.Any suggestionsPS- Tried .ToString();
S3THST4
What's wrong with the c# project I linked you to above, complete with source code? Is that not what you're looking for?
Mystere Man
Updated to show how you can then write the file to disk using System.IO.File.WriteAllBytes
Shawn Miller
Thank you, a lot, smiller.
S3THST4
A: 

Hi, in case you simply want to store multiple files in a single file storage (and extract files from there, interact etc.) you might also want to check out NFileStorage, a .net file storage. written in 100% .NET C# with all sources included. It also comes with a command line interpreter that allows interaction from the command line.

A: 

System.IO.File.WriteAllBytes(@"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);

Doesn't work for me!

On "WriteAllBytes" it says: cannot convert from 'System.Drawing.Bitmap' to 'byte[]' for image and cannot convert from 'string' to 'byte[]' for text file.

Help!

For me it is actually useful to store some files in EXE to copy to selected location.

You're better off posting this as a separate question. More people are likely to see your question and answer it well if you ask a new question than if you hide a question in the answers to another question.
sth