Click here to Skip to main content
13,295,051 members (65,991 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I just created my first DLL using C# in MS Visual Studio 2010. My executible was also created in C# and everything works fine. The only issue I am having is, my program only runs if the DLL is in the same folder. Please tell me what I need to change in either my program or dll so I will be able to place the DLL in the C:\Windows\system32 folder and run my program for a differnt location.

Thank you
Posted 5-Nov-12 12:47pm
Good question, something which is good to know; my 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

You could also use this event to figure out where to load your dll from:


From time to time, I'd like to work with third party dlls and I embed them in the executable to create a standalone executable that could just work anywhere. Using this event allows me to reach embedded DLL's and then use them at runtime.

AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
				string asmname = (new AssemblyName(e.Name)).Name;
				string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
				string resname = (from p in names
								  where p.Contains(asmname)
								  select p).FirstOrDefault();
				byte[] buffer = null;
				using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resname))
					buffer = new byte[stream.Length];
					stream.Read(buffer, 0, buffer.Length);
				return Assembly.Load(buffer);
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

In that case you must register the DLL in the GAC (Global Assembly Cache)
like this:

gacutil /i mydll.dll

Open a visual studio console(as admin) and run the gacutil.exe utility.

see more details here:[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

With dotNet, you'll want to have the assembly in one of two places, either the Global Assembly Cache (GAC), or in the same directory with your application.
The only reason for an assembly to be placed in the GAC is if your assembly is going to be used by multiple different applications. You will have to have your assembly strongly named as well in order to put it in the GAC.

To get a better understanding of the GAC, read this[^] documentation on it.
Sorry, this is not exactly so. Also, this way is quite reasonable. It's not always practical to put everything in GAC; and I explained the use case where one would not want to put all in the same directory. There are other good solutions -- please see my answer.

(I did not vote...)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Yes, you may need some library assemblies in a different folder. For example, you need it if you have some different applications using shared libraries and you don't want to mix them together or put anything in GAC. Here is one of the methods based on application config file:

Let's assume the main executable module of your entry assembly is "myApplication.exe", then you can create the file "myApplication.exe.config" with something like:
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<probing privatePath="..\MyLibraries\MySharedAssemblies"/> <!-- or whatever directory you need -->

If you do this, you can place your shared assemblies in the directory "..\MyLibraries\MySharedAssemblies" (can be anything else). In this example, the path is relative to the location of the main executable module of your entry assembly and config file, but it could be anything, including absolute path, but relative is more manageable.

There are other methods of the resolution of assembly location; I prefer this one.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100