Anatolii Humennyi
Anatolii Humennyi

Reputation: 1856

Set custom file name to referenced dll

I have an application, which references to assembly "Library.dll". I changed the name of this assembly to "Library222.dll" and now my application fails with an exception "Could not load file or assembly ..." How to specify new name "Library222.dll" of this dll-file in runtime? I found this question Set Custom Path to Referenced DLL's? but there specifying folder to dll, not file name. I didn't change path to dll, I changed file name, so I need to specify file name.

Upvotes: 3

Views: 2888

Answers (3)

sukhi
sukhi

Reputation: 924

try this go to Project Properties -> Application, and then change the Assembly Name field. On earlier versions it might be in a different place but I think the Assembly Name field is still the one you are looking for.

Upvotes: 2

Anatolii Humennyi
Anatolii Humennyi

Reputation: 1856

I found this simple solution! The event AppDomain.AssemblyResolve has helped me solve the problem

using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace TestAsembly
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            //bla-bla...
        }

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            if (args.Name.StartsWith("Library,"))
            {
                Assembly asm = Assembly.LoadFrom("Library222.dll");
                return asm;
            }
            return null;
        }
    }
}

Upvotes: 3

Stefan Steiger
Stefan Steiger

Reputation: 82186

You cannot achieve this by only renaming the assembly.

The name of an assembly is written into its meta data at compile-time.
When you change the file's name, you do not actually change the name in the metadata.

You have to unreference Library.dll, and reference Library222.dll, and then recompile.

Upvotes: 5

Related Questions