Unzipping on the fly in C# - c#

I have built a console application that works okay when it references a .exe file from a Program Files, but my users may not have that .exe in their Program Files directory.
I would prefer to keep the package as a single .exe for simplicity, so I was wondering how I can combine the two .exe's into one package.
One thing I thought of is zipping the .exe from the Program Files directory to a temporary location, and I would store the binary data for the zip archive in my console applications source code. Is this the best way to do it or are there better methods?
Note I do not have the source code of the .exe I want to reference in my console application.

You can certainly store extra files in your .exe (or .dll) as embedded resources. Simply change the "build action" for the item in the project to "Embedded Resource". You can retrieve the contents of the file (which could be compressed, if you wished) by using the following:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("stream name")
You could extract the file onto disk to be able to reference it, or you could load it directly with one of the Assembly.Load() variants, so you wouldn't need to ever store it on disk.
Note that if you do choose to extract it and store it on disk, you'll need administrator permissions on Vista and Windows 7 (and properly administered XP) operating systems in order to save the file(s) to the Program Files directory.

You can use GZipStream to compress and decompress files in C#. For more complex compression, you can use other Zip libraries like SharpZipLib.

Take a look at this link: Embedding assemblies inside another assembly
Basically, ILMerge will do what you are asking.

Related

How to make an updater for my WPF application? Installing only upgraded files

I finished my app and now I want to create a update system. I have the installer so I can install the application in other machines, it was made with InnoSetup (I don't know if it is the best way, but it worked). I know the basics, I have to compare the current version with a string stored in a in a web server, if it is greater, download the files. Now, what files? Because the InnoSetup gives me these files:
Where are the .xaml (design) files? And the .cs files? Are they compressed in the .exe? For example If I add a few lines to a class, I want to download this class, no the full installer again. Because the final size of my application is 30Mb, if I change some things of a class, I do not want the user to have to download the 30Mb again
In the most basic of terms, when you compile your program the compiler turns your cs and xaml files into machine readable code and puts it all into an exe file.
Yes, if you add a few lines to a class and recompile it, it will rebuild your exe (assuming the class is part of it, and not an external library).
You still have dependency dll files that you need to include, and any other external content that you've included. But once you have all the external files installed, you wouldn't theoretically need to download them again on an update. Only the files that you've updated, ie the exe.

How do you "Build" an exe that includes all the resource files like pics? Can exe be put on a web page?

I have students in high school who have created some programs using Visual Studio C#. They created some games and would like to now upload them to the web. I am pretty new to Visual Studio C#. I thought after a program was "built" that you could go into the bin/debug folder and get the exe file for the program and be able to play the game without having to have Visual Studio on the actual computer you are playing it on. It works with some of their games but some of them, there are graphical files that are missing in the exe file if all the other files aren't stored in the same place. How can I get a clean exe of their game? Can that exe be loaded up onto a web server so they can play it from there or at least download it from there?
Microsoft wrote a guide on how to do exactly this. It's on their MSDN website, which is sort of like the developer back-bone for a lot of Microsoft software and documentation.
Old guide: Adding and Editing Resources (Visual C#)
Newer guide: How to: Add or Remove Resources
The gist is that the program needs to know where the files are, relative to the location of the compiled binary (in this case, an .EXE); There's several different ways to do this, depending on your level of expertise. I would suggest that you take a look at the guides above to start that journey.
there's a few ways of doing this. (Also, make sure you're creating a 'release' build when you compile).
You can include resources in your game by creating a resources file. This is something I usually do only on winforms applications etc.
If you have on-disk files you need to distribute those on-disk files along side your game. You could do this by zipping up your game.exe and the /files/images.img folder (or what ever your resources content folder is named).
If you're feeling adventurous you could create a 'deployment project' which is a project that allows you to create an installer file. This is a bit more work however you will have fine grained control over what files go where etc.
Good luck!
EXE files generally aren't self contained for video games with many resources, just add resources in an external folder and make sure the paths to the resources in their games are not absolute but relative in the local directory.
So for example:
get rid of paths like C:/Users/Bob/Desktop/Game/Images/player.png and replace with /Images/Player.png.
Also, an EXE generally doesn't run client side on webpages, it is possible but difficult. Things such as Flash are made for this sort of issue. I'd say make the webpage a place where they can download their games. Github has a nice way to do free websites called gh-pages. OFC, you could host one yourself but that would require a lot of setup work.
Like Monza said, you can zip up the files for download. Or, you could create an installer if you wanted to be really ambitious.
I thought after a program was "built" that you could go into the bin/debug folder and get the exe file for the program and be able to play the game without having to have Visual Studio on the actual computer you are playing it on.
That is correct, given that the other machine has the necessary .NET framework version installed AND any other resources like .dll files and config files are also present. When the application is ready for release, you can set the Build options in the project properties to Release, and then when you build all the files needed for distribution will be in the bin\Release folder.
It works with some of their games but some of them, there are graphical files that are missing in the exe file if all the other files aren't stored in the same place.
This may depend on how the application was written. If resources like pictures are embedded in the assembly then those files will not be needed to run on another machine. If the application is using hard coded paths for the image resources in the source code, then the application will likely break if the necessary files aren't present when the .exe is executed.
Can that exe be loaded up onto a web server so they can play it from there or at least download it from there?
Yes - you could upload the .exe to a file server to make available for a download, but you would not be able to run it within a browser over the web. I would recommend zipping up the .exe and other files needed and hosting the .zip file on the web server for download; browsers may give a warning or block downloading an .exe .
Hope this explains it a little bit for you.

Compiling folders into Executable

Maybe not possible but I have an application that gets text from a bunch of text files organised in folders which are all included in my project. However I'd love to compile these text files into the exe so that I don't have to lug the folders with the application. Anyway I can do this?
The folders are in a hierarchy i.e. each folder has a txt file and image that my application uses. At the moment they are all set to build as Embedded Resources which I thought would have compiled them in but unfortunately not.
Preferably I love if there was a way I could add the folder rather than each file individually.
"Compilation" means something else.
Anyway, .NET confusingly uses the same terms to describe "embedded resources", which are exposed as resource streams from the assembly, and the more API-friendly resx resources which are then compiled into .resources files that are also stored as embedded resource streams.
If you want to use ResX resources in your project then go Add New Item > Resources (resx), then go to the Files tab and add references to your filesystem files, then build your project. You'll be able to access those files by going typing FooResources.MyFileName in your program's code, assuming you named your resx file FooResources.

C# Xml files when creating exe application

I'm planning to build my winform into a .exe file. I'm just wondering what to do with the XML files that my application needs?
I did some research and found out that I can add the XML files in the Resource folder before creating a .exe file.
Or I need to create a setup file? When the user runs the setup file, the XML files will be installed into their pc.Now I wonder which one is the best way to go for,
Note: XML files might get modified by the user.
If you want to ship the XML files as seperate to the .EXE then you can set the Copy to Output Directory to Copy if newer. (click on file and then go to properties).
OR if you want it as part of the .EXE I think you can change the Build Action to Embedded Resource.
I personally would create a Setup as per your edit and include the XML files. I usually just add everthing from the bin/release folder that is needed when I create a setup file.
You could either deploy the necessary files along with the executable in the same folder or embed them as resources (if they are read-only). If you need to modify them do not embed them as resources into the executable.
The correct way depends on how you intend to use the files. If the files always are deployed together with your application, the application never writes to them and they are never upgraded without upgrading the application, you can go with them embedded as resources.
If you need to update them separately from the application, you need to have them as physical files.
You don't necessarely need a installation package, unless you need to apply some logic during setup, such as updating the content of the setup based on user input or to check preconditions. The application can just be copied into place regardless of if you have embedded the files or not.

How to Use a Windows App with Embeded files and folders to copy to a destination?

I am trying to write a small application, whose only purpose is to copy some folders and .cs source files into a user specified Directory, I can do it easy enough by simply having the application look for the files and folders in its own install directory then copy them to thier destination Directory, but I was wondering if its possible to Embed the Folders and Files into the Application, so that when you run the application it creates or copies the folders and files from the exe app directly to the install directory, rather than searching for them in the apps install directory then copying them over. Basically Im trying to only have a single exe file rather than having an exe file and a bunch of folders and files along side it.
Is this possible to do with just a Windows Form App without using an actual Installer Class?
Yes. Embed the files into the application executable as embedded resources. Then when your application runs, access the embedded files and write them to disk in the desired directory structure.
Here is an example of how to embed and access embedded resources from your application assembly.
http://support.microsoft.com/kb/319292
Sure you can, use the BuildAction property as Content or Resource.
Depending on the number and structure of files/folders, you may also consider embedding one zip file and extracting it with sharpziplib or some such.

Categories