Mr_BlackHawk7 Mr_BlackHawk7 - 1 year ago 89
C# Question

Is it possible to add a library as a resource C#

I am using the iTextSharp pdf library to create pdf files in C#. Is it possible to add the library file into the ressources (under "projektname"-properties) so I can simply give the users the .exe without them installing the programm.


This is the error i get when i try to simply run the exe from the debug folder without installing it (sorry its german).

error box

How can i add the files and will it work or is there a better way to do it.

Answer Source

Yes you can:

  1. Open the project properties, go to the resources tab and select "Add Resource > Add existing file" ("Ressource hinzuf├╝gen > Vorhandene Datei hinzuf├╝gen"). Select the DLL. It should appear somewhere in the resources, either under "files" or under "other".
  2. Copy it from the resources to file as follows

In your code, check whether it is present when the application starts. The following assumes you're using WinForms. If not, the code can also be added to the WPF App class. There's a method you can override that's called when the WPF application starts.

static void Main()

    string dllPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "filenameofdll.dll");
    if (!File.Exists(dllPath))
        byte[] fileBytes = Properties.Resources.<resourcename>;
        File.WriteAllBytes(dllPath, fileBytes);  

    Application.Run(new Form1());

<resourcename> in the above code will be the name of your resource. It should be auto-completed by IntelliSense.

This should work provided that the DLL is only used after that above code executes. The DLL should only be loaded when the first instance of a class from the DLL is created, so you should be fine.

I do not know whether this has licensing implications! Please make sure that you're not doing anything illegal. Also: The above will only work if the application has write permissions to the folder.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download