I have a C# project with a bunch of dependencies, but I want to store them in another folder from where the executable is.
For example
WTEngine/
/bin
--Dependencies go here
/otherfolder
WTEngine.exe
How do I do that? I move them and the executable can't find them anymore
This answer for ASP.NET addresses the same issue, and most of the answers can be effectively ported to a desktop executable: How do I reference assemblies outside the bin folder in an ASP.net application?
Also see How do I change the lookup path for .NET libraries referenced via #using in Managed C++?
The .NET dependencies are loaded automatically from two places:
GAC (http://en.wikipedia.org/wiki/Global_Assembly_Cache)
the current path of the executing assembly (your exe)
You cannot move the dependencies in another folder and have them loaded automatically.
If you decide to load the assemblies manually, check out this question:
How to Load an Assembly to AppDomain with all references recursively?
Related
I am studying about assemblies and confused on some points after searching I am asking question here. I read about assembly resolution that for strongly named assemblies the order of assembly resolution is to first check GAC then the root directory and then private probing.
Suppose I have a solution with multiple projects and within this there is a folder to place all the reference assemblies lets say log4net and in project where I need to use log4net assembly I add reference of log4net dll pointing to the folder within application solution directory where all referenced dlls have been placed. So why CLR check GAC and then root directory etc why not from the path mentioned while adding reference.
I think you mean "in Visual Studio" when you say "add reference to this".
What will happen when you run the program on a machine where this path does not exist?
Btw. the "Add reference" in visual studio is only used at compile time. It will copy the referenced dll to the target folder.
Visual Studio creates a target folder (Standard bin\Debug or bin\Release) in the project folder and stores there all files (exe, dll, config) for deployment.
Installing DLLs into GAC has to be done manually and makes sense only if it is a DLL which is used by several applications and should be used carefully.
You could get strange and difficult to find errors when your application loads a dll from GAC and this dll has another version as expected by your application.
I have a C# project which builds a library DLL, myLib.dll. This DLL has a dependency on a 3rd party DLL, dep.dll (which I provide but do not build).
I want others to be able to use my library by adding my project to their solutions and referencing it. Adding my project as a reference automatically copies myLib.dll to the Target directory of their app, but of course dep.dll is not copied with it.
Bear in mind that I have no control over where my project is in their code tree, and the DLL can't know where it was copied from.
The only solution I can see is to force the user to add a build event which copies dep.dll to their target directory. We can't assume users can create environment variables.
This is very undesirable for several reasons (it's not really one dependency - I simplified things, I don't want them to have to be concerned about the dependencies in my project, and it's very fragile - adding an extra dependent DLL to my project would require everyone to update their build scripts).
I can't load the dep.dll directly as I don't know where it is - my build scripts can copy it to the same target directory as myLib.dll but the actual version of myLib.dll they run will have been copied somewhere else.
I also can't make dep.dll a reference directly (probably because it's not a .NET assembly). I just get "Error loading code-completion information for dep from dep.DLL. Format of the executable (.exe) or library (.dll) is invalid".
So is there any way to have the dependent DLL just seamlessly copied with myLib.dll when a client builds their application? [I'm using Sharp Develop if it matters.]
Can you add dep.dll to your projects root folder as a file like a .cs file. Set the Build Action to None and Copy to Output Directory to Copy if newer.
I have some ASP.NET application which composed of some modules. The web application has not direct reference to these modules. But in some part of its code it Loads those assemblies using following code in MyWebApplication.dll:
// modules: Module1 (Module1.dll), Module2 (Module2.dll), ...
foreach(module in modules)
{
Assembly.Load(module)
}
But when I'm running the application, it can't load the module.
To debug it, I checked where the other assemblies are located using:
typeof(SomeType).Assembly.Location
and the result was:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\99db3519\a290da98\assembly\dl3\b95f323e\b38a9c7e_53bdd001
In the following folder
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\99db3519\a290da98\assembly\dl3
there are the other assemblies each one in a separate folder. But it seems that ASP.NET brings just the required dll files here. And it seems that it brings the assemblies that are referenced in some way from the WebApplication.
Unfortunately my module assemblies are not referenced from WebApplication as they're being loaded dynamically at run time.
Question: How can I say to ASP.NET to copy and bring my modules too, so I can load them at run time?
Unless you concerned with unloading assemblies use Assembly.LoadFrom with full path.
To get full path in ASP.Net use Server.MapPath (see following question to get correct arguments: Server.MapPath("."), Server.MapPath("~"), Server.MapPath(#"\"), Server.MapPath("/"). What is the difference? ).
Notes:
you can't unload assemblies unless using custom separate AppDomain (just warning, probably not concern in this case)
Load, LoadFrom and LoadFile have very subtle differences - when manually loading assemblies make sure to carefully read MSDN articles for corresponding methods, related question on SO like Difference between LoadFile and LoadFrom with .NET Assemblies? and Suzanne Cook's articles (not much changed with these methods since 2005).
You can copy your modules to the project's bin directory in a post build event.
You can create a folder in your web application called "DynamicReferences" and store a copy of each dll in that folder. Then for each dll in that folder set the Copy to Output Directory property to "Copy Always" or "Copy if Newer"
I need to use an external DLL library within my project. When I add the reference to that library, is my project importing it, or is it just referencing the DLL at its location?
At compile time, it is a reference to the dll at that location. However once compilation is done, the referenced assemblies may be copied to output directory of your project i.e. next to your project's dll or exe e.g. bin\Release (configurable). You can control this behavior by changing the "Copy Local" property on the reference.
At run time, the assembly is searched for in multiple places such as; the same directory where your primary executable/assembly is placed. See Locating the Assembly through probing for details.
.NET does not embed the referenced assembly in your exe/dll unless you use ILMerge.
Define what you mean by "import"? Where you think it is imported to? It is a reference. The file gets copied to the output when the reference is told to do that.
When I reference DLLs from outside of my project, I have found that the dll file gets copied into by bin/Debug or bin/Release folder. Then, links are resolved at runtime, so while by .exe can be moved around, I still need those DLLs in my path or else I get a "missing reference" error.
This has bitten me a few time when running a C# project via mono on a Linux distribution until I remembered to copy the .dll files over as well. Then it worked fine.
I have a .dll file in my project folder and would like to load it via Assembly.Load().
AssemblyName name = new AssemblyName("Portable.Store, Version=0.1.0.0, Culture=neutral, PublicKeyToken=12ay62c33eocf6uf");
Assembly assembly = Assembly.Load(name);
However this would throw a FileNotFoundException due to not specifying a path. And I am unable to use Assembly.LoadFrom() or Assembly.LoadFile() because Portable Class Libraries only support Assembly.Load()
Is there a way to do this inside a pcl? Any help is appreciated, thank you!
Edit #1: Would it matter if the assembly I'm trying to load is a non PCL? I know that this defeats the purpose of the PCL however there are a few libraries that are not included in the PCL. Therefore using conditional compilation, depending on the platform, I will load platform specific assemblies.
Edit #2: Found some more information on where the dll should be placed: https://stackoverflow.com/a/6440406/2464165
As of now I just placed it inside my project folder, with the .sln file and what not Where exactly would be the app probing path?
Edit #3: I was able to get my dll file placed inside the Resources Folder of a .dll file. So I have MyPCL.dll and inside that is where I have the ResourcesFolder/Portable.Store. How could I tell the Assembly.Load to look in specific folders instead of just the main AppX root directory?
I'm making the assumption that you are running the portable library in a Windows Store application (based on the assembly name you are trying to load).
There are two places that store apps find their assemblies, either in GAC if it is a framework assembly or the Appx package if it is a user assembly.
As "Portable.Store" (which I assuming is from my PclContrib project) is a user assembly, it must be loaded from the AppX package. To ensure that both assemblies end up in the AppX package, simply make sure that the Windows Store project containing the AppxManifest references both of them. That's it.
If Assembly.Load still cannot find the assembly, check to make sure that the strong name you are passing to Assembly.Load is correct.