Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
4.33/5 (5 votes)
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
Comments
Sergey Alexandrovich Kryukov 5-Nov-12 17:55pm    
Good question, something which is good to know; my 5.
--SA

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:
HTML
<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
 
Share this answer
 
v2
You could also use this event to figure out where to load your dll from:

C#
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.

C#
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);
			};
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 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
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900