Ufuk Hacıoğulları Ufuk Hacıoğulları - 1 year ago 46
C# Question

How do I reference dlls when "added as link" in project?

I have two dll files in the project tree(not the references). They are added as link, they are assemblies of other project in solution. I'm trying to set their Build Action to Embedded Resource, so I can import them to .exe file. I can't write using statement, so I can't reference them in current project. How can that be done?

project file

Answer Source

You need to add a hard reference to the assemblies and set their Copy Local to False, then extract the assemblies from your embedded resources to the application directory before they are invoked. You can't reference a linked (shortcut) like you want.

Key Points (in this example) and the Blog Article with Example Code

  • EmbeddedReferenceApplication hard references EmbeddedReference.dll
  • EmbeddedReference reference property Copy Local is set to False
  • Linked assembly (Add as Link) is set as Embedded Resource

Here is a working example. (EmbeddedReferenceApplication.exe | Console Application)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using EmbeddedReference; // Hard reference with Copy Local = False

namespace EmbeddedReferenceApplication {
    class Program {
        static void Main(string[] args) {
            AppDomain.CurrentDomain.AssemblyResolve += AppDomain_AssemblyResolve;

        private static void MyMain() {

        private static Assembly AppDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
            string manifestResourceName = "EmbeddedReferenceApplication.EmbeddedReference.dll"; // You can also do Assembly.GetExecutingAssembly().GetManifestResourceNames();
            string path = Path.Combine(Application.StartupPath, manifestResourceName.Replace("EmbeddedReferenceApplication.", ""));
            ExtractEmbeddedAssembly(manifestResourceName, path);
            Assembly resolvedAssembly = Assembly.LoadFile(path);
            return resolvedAssembly;

        private static void ExtractEmbeddedAssembly(string manifestResourceName, string path) {
            Assembly assembly = Assembly.GetExecutingAssembly();
            using (Stream stream = assembly.GetManifestResourceStream(manifestResourceName)) {
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                using (FileStream fstream = new FileStream(path, FileMode.Create)) {
                    fstream.Write(buffer, 0, buffer.Length);

In EmbeddedReference.dll

using System;
using System.Collections.Generic;
using System.Text;

namespace EmbeddedReference {
    public static class MessageHelper {
        public static void ShowMessage() {
            Console.WriteLine("Hello World!");