Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 11:47am
Comments
Sergey Alexandrovich Kryukov at 5-Nov-12 17:55pm
   
Good question, something which is good to know; my 5.
--SA
Rate this: bad
good
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:
<configuration>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<probing privatePath="..\MyLibraries\MySharedAssemblies"/> <!-- or whatever directory you need -->
		</assemblyBinding>
	</runtime>
</configuration>
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.
 
—SA
  Permalink  
v2
Rate this: bad
good
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:
http://msdn.microsoft.com/en-us/library/ex0ss12c(v=vs.80).aspx[^]
  Permalink  
Rate this: bad
good
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.
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 5-Nov-12 17:58pm
   
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...)
--SA
Rate this: bad
good
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:
 
AppDomain.CurrentDomain.AssemblyResolve
 
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);
			};
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 407
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,632
1 Sergey Alexandrovich Kryukov 5,429
2 Maciej Los 3,474
3 Peter Leow 3,309
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2014
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