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.
Asked
Active
Viewed 2,657 times
3 Answers
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.

Stefan Steiger
- 78,642
- 66
- 377
- 442
-
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... – Anatolii Humennyi Sep 30 '13 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;
}
}
}

Anatolii Humennyi
- 1,807
- 3
- 26
- 37
-
1this 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 '13 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. – Anatolii Humennyi Sep 30 '13 at 12:46
-
2if 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 '13 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. – Stefan Steiger Oct 01 '13 at 05: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.

sukhi
- 904
- 7
- 11
-
Thanks for the reply, but I wanted to do it in runtime and I already found a solution (see my own reply below) – Anatolii Humennyi Nov 05 '14 at 16:19
-
This answer assumes the developer has the ability to compile the DLL in question. – Jay Sullivan May 09 '18 at 14:26