Self-Extracting executable files with C# and VB.NET

In this chapter we are going to discuss the creation of self extracting password protected executable files (SFX EXE file). This feature was request by one of our existing customers and the main idea is that it enables you to send OpenPGP password encrypted data to someone who does not have to have any OpenPGP software installed.

The classes that provide this functionality are located at the DidiSoft.Sfx namespace.

1. Example
2. Target .NET Framework
3. Customization

1. Example

Below is an example that illustrates how to encrypt a single file into an OpenPGP password encrypted EXE file.

C# example

using System;
using System.IO;
using DidiSoft.Sfx;
class SfxFromFile
    static void Main(string[] args)
        // decryption password
        string password = "pass123";
        // create the self extracting file
        SfxCreator sfx = new SfxCreator();
        sfx.TargetDotNetFramework = SfxCreator.DotNetFramework.SELECT_FROM_EXECUTING_PROGRAM;
        sfx.EnryptFileToExe(new FileInfo(@"DataFiles\test.txt"),
                            new FileInfo("sfx_demo.exe"));

VB.NET example

Imports System
Imports System.IO
Imports DidiSoft.Sfx
Module SfxExeFromFileDemo
    Sub Main()
        ' decryption password
        Dim password As String = "pass123"
        ' create the self extracting file
        Dim sfx As New SfxCreator()
        sfx.TargetDotNetFramework = SfxCreator.DotNetFramework.SELECT_FROM_EXECUTING_PROGRAM
        sfx.EnryptFileToExe(New FileInfo("DataFiles\test1.txt"), _
                            password, _
                            New FileInfo("sfx_demo.exe"))
    End Sub
End Module

A screenshot of the started sfx_demo.exe can be seen below:

The SfxCreator class provides also methods for encrypting a whole folder with its contents.

2. Target version of the .NET Framework

By generated EXE file is a managed executable that requires .NET Framework on the target machine. By default the file is compiled with the version of the Framework that the current process is running under.

You can select explicitly from version 2.0 and above.

3. Customization

Customization is achieved with the help of the DidiSoft.Sfx.SfxOptions class, when it is passed as parameter to the SfxCreator class methods.

The title and icon of the application can be set with the properties SfxOptions.WindowTitle and SfxOptions.IconFile.

If you need additional customization of the look and feel of the resulting application, you have to:

1. Unzip the file [library setup folder]\Help\
2. Modify with Visual Studio the form file FormMain.
3. Specify that you wish to use the modified self extracting sources in the SfxOptions.SourceFolder property.