1

I need to create a lightweight .Net application which can be distributed easily (via email attachment, for example). I can run the EXE from a ZIP, however it seems I cannot get the EXE to reference any external configuration files (such as an exe.config, or any other XML file) when run from the zip.

Is it not possible to run an EXE from a zip file when that EXE uses data from the exe.config file?

I have reviewed this question already:

.NET windows application, can it be compressed into a single .exe?

but I think he is trying to solve a different problem than I am.

I believe my problem is similar to this: Wrap files in one executable package

However the answers for this question have provided a clear solution.

Community
  • 1
  • 1
JohnZaj
  • 3,080
  • 5
  • 37
  • 51

2 Answers2

3

It is not possible because the other files in the ZIP need to be extracted before they can be used by the .exe.

Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • That makes sense. In terms of a solution, I feel I need a tool to pack the EXE and its related files into a single EXE. Seems like NBox might help: http://nbox.codeplex.com/ – JohnZaj Mar 20 '11 at 04:21
  • Seems like a good solution on the box, hopefully it works for you! – Prisoner Mar 20 '11 at 04:23
3

Is it possible? Well let's look at the situation here.

A zip is an archive format. It does not specify a magic number; instead a zip file is identified by the presence of marked records (zip entries) present anywhere in the file.

In windows, an EXE is a file format defined by a magic number. It allows for the inclusion of arbitrary data and specially-marked places in the file.

Combining the two, it is possible for file to be both an exe and a zip. This possibility is exploited in producing self-extracting zip files (SFX): an exe which reads itself as a zip file and extracts the entries contained within. An SFX is basically comprised of some "stub" exe logic that knows how to read and extract any zip file, plus a variable set of zip entry records.

Your requirement - an archive format that extracts itself, then runs a particular file from the extracted content - requires an SFX stub that is only slightly more capable than the standard one. It must extract files, then run one of those files.

DotNetZip is a .NET-based zip library that allows your .NET app to produce zip files, including SFX files. The SFX stub provided by DotNetZip includes the ability to invoke one of the extracted files, after extraction. The command-to-run-after-extraction need not be a .NET executable, but it could be. Of course, since the exe and its config file have been extracted, the exe can run successfully, with access to the .config file. This should meet your requirements.

In code using DotNetZip to produce an SFX that runs a post-extract command looks like this:

string DirectoryPath = "c:\\Documents\\Project7";
using (ZipFile zip = new ZipFile())
{
    zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath));
    zip.Comment = "This will be embedded into a self-extracting console-based exe";
    SelfExtractorOptions options = new SelfExtractorOptions();
    options.Flavor = SelfExtractorFlavor.ConsoleApplication;
    options.DefaultExtractDirectory = "%USERPROFILE%\\ExtractHere";
    options.PostExtractCommandLine = "%USERPROFILE%\\ExtractHere\\MyApp.exe";
    options.RemoveUnpackedFilesAfterExecute = true;
    zip.SaveSelfExtractor("archive.exe", options);
}

DotNetZip is free to use.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • DotNetZip looks promising. Nice work Cheeso! Quick question: The RemoveUnpackedFilesAfterExecute property's description: "Whether to remove the files that have been unpacked, after executing the PostUnpackCommand." Where is this "PostUnpackCommand", I cannot find it in the doc? – JohnZaj Mar 20 '11 at 15:55
  • maybe a documentation error. It should be *PostExtractCommandLine*. After running the command, do you want to remove the files that had been extracted? – Cheeso Mar 20 '11 at 15:57
  • That is what I suspected. I'll tell send dpchiesa a comment to fix...kidding. And yes, I want to remove the files. I"m going to give this a test and see if I can get what I need out of this library. – JohnZaj Mar 20 '11 at 16:04
  • 2
    What reference is required for `SelfExtractorOptions`? – Sunil Agarwal Jul 03 '12 at 10:52
  • Ionic.Zip.SelfExtractorSaveOptions options = new Ionic.Zip.SelfExtractorSaveOptions(); – boomdrak Nov 19 '18 at 18:05