3

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.

3 Answers 3

5

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.

1
  • I do not need to change the name written into its meta data at compile-time. There is a name still remains "Library.dll". I need to set new assembly name in runtime, but you offer me change it before compiling the project... Sep 30, 2013 at 12:19
3

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;
        }
    }
}
4
  • 1
    this feels like a very odd work around. What if there are other libraries that start with "Library,"? Are you sure that you shouldn't recompile with the correct linkage to the correct file instead?
    – default
    Sep 30, 2013 at 12:38
  • @Default Because the name of the dll can be changed by the user after he install my program. Library Library.dll, which I cited the example - it's kind of plugin. Sep 30, 2013 at 12:46
  • 2
    if the user can change the name I guess he can change it to "Library223.dll" as well. That would halt your program and you'd need to recompile again with a new name. For me, that's the same as referencing correctly and not letting the user rename dependent dlls. It just doesn't feel right... But hey, if it works for you.
    – default
    Sep 30, 2013 at 13:00
  • 1
    @Default: True, but now he can add the entries as a section into the web/app .config file, and read it from there, then it becomes dynamic. Oct 1, 2013 at 5:48
2

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.

2
  • Thanks for the reply, but I wanted to do it in runtime and I already found a solution (see my own reply below) Nov 5, 2014 at 16:19
  • This answer assumes the developer has the ability to compile the DLL in question. May 9, 2018 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.