34

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?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
S3THST4
  • 1,025
  • 2
  • 10
  • 8
  • out of curiosity, why are you programming your own installer? – Serge Wautier Feb 08 '09 at 21:25
  • 39
    People always tell you to "Code code code if you want to learn a language" until you actually code something then they are all DON'T REINVENT TEH WHEEEEEEL – Rayne Feb 08 '09 at 22:03
  • 1
    There's nothing wrong with coding to learn, but when writing production code one shouldn't reinvent the wheel. – Erik Funkenbusch Feb 08 '09 at 23:13
  • 1
    When you program an installer it's not reinventing the wheel because the "wheel" hasn't been invented yet. All installers are just squares no one has invented the perfect installer yet. – Bitterblue Jan 29 '13 at 10:41
  • 3
    @Rayne, bless you for that comment! My next favourite is when they go "Why would you even want to do that?" – mathgenius Sep 20 '17 at 19:14

4 Answers4

33

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
  • 7,082
  • 6
  • 46
  • 54
  • 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 suggestions PS- Tried .ToString(); – S3THST4 Feb 09 '09 at 04:56
  • What's wrong with the c# project I linked you to above, complete with source code? Is that not what you're looking for? – Erik Funkenbusch Feb 09 '09 at 06:33
  • Updated to show how you can then write the file to disk using System.IO.File.WriteAllBytes – Shawn Miller Feb 09 '09 at 16:23
  • Thanks for this, I'll probably use this for a config tool that lets the user choose between different DLL overrides. This way I'm sure I don't need to manipulate external files in the app folder that users might mess with or delete. – Nyerguds Dec 28 '10 at 14:20
9

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#.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • 1
    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 Feb 08 '09 at 21:30
  • 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 Feb 08 '09 at 21:57
  • 1
    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 Feb 08 '09 at 22:30
4

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
  • 28,894
  • 13
  • 84
  • 111
0

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.