How to move referenced dlls to another folder C# - c#

I'm trying to use CefSharp's Chromium Browser, but I don't want a clunk of files from cefsharp in its startup folder. I want to keep it in the bin folder, I don't know how.
Sorry, I'm a newbie at VS and all.

This is a partial solution to your problem. You can use the <probing> element in your run-time configuration, for specifying where to search for your assemblies, like in this example below, taken from the documentation:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>
EDIT:
The rest of the answer is in this post here.

Related

Is it possible to separate a C# executable and its dependent dlls in different folder and run the application?

I am working on a feasibility task, where it is required to keep a C# - WPF application in a folder and to keep its dependency dlls in a separate folder. Is it possible to do dynamic loading ?
When I investigated I found that .NET assemblies should be placed in a common directory/sub directories due to security reasons, otherwise it wont work. My requirement is to separate them in a different drive-folder.
Can you suggest a solution for this?
Yes it is possible according https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/probing-element?redirectedfrom=MSDN. You will have to update your configuration file (app.config which gets copied to your.exe.config) with something like this:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>

How to chage default directory of .dlls creating after built with runtime and probing?

I want to move all .dlls to "lib" folder after building my application. Default program creates them in directory:
bin\Debug\netcoreapp2.2
And this is directory where I want to place my .dlls:
bin\Debug\netcoreapp2.2\lib
I create script which moves my .dlls to this folder and put it in post-built event. It works correctly.
Next, I add runtime to my app.config file like I saw in many tutorials and articles on stackoverflow.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib"/>
</assemblyBinding>
</runtime>
But, after building and starting my program, console says:
The application to execute does not exist:
app\bin\Debug\netcoreapp2.2\myDLL.dll
I want to know what's wrong with my application? How can I change the directory in which program is looking for .dlls?
I guess what you need is the codebase element in the configuration.
From documentation, If the assembly has a strong name, the codebase setting can be anywhere on the local intranet or the Internet. If the assembly is a private assembly, the codebase setting must be a path relative to the application's directory.
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<codeBase version="2.0.0.0"
href="http://www.litwareinc.com/myAssembly.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Hope this helps.

Use a specific version of a dll in C#

I have a project where I would want to use some specific version of a dll.
The GAC contains couple of versions of that dll (new & old), I would want to use the old when running the program.
Issue is that the newest dll is always picked-up from the GAC.
Would you know if there is a way to either:
Force the usage the dll that is in the run folder (the one I'm referencing in my solution, working fine in debug).
Force the usage of the old version of the dll from the GAC.
Thank you!
You can use a binding redirect in your app.config or web.config in the runtime node:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Make sure you have the correct publicKeyToken and know which versions you want to redirect to what version.
(You can check a publicKeyToken of a DLL like this with this info.)
MSDN Documentation
You can also generate these for an entire solution using the Package Manager Console
Get-Project -All | Add-BindingRedirect
This will update all app.config files and add the binding redirect.
When you have added the library to your project and you collapse the 'References'-node of the project tree, you'll see the added library. When you select it and click the 'Properties'-node of the context menu, you can specify if a specific version of the library should be used and which version to use. Simply set 'Specific Version' to true and specify the Version number. Then you don't have to cope with the question where the version you want is loaded from.
Have you try to "Redirecting Assembly Versions" in your app.config? http://msdn.microsoft.com/en-us/library/7wd6ex19(v=vs.110).aspx

How to dynamically specify the path to the class library DLL?

Target: dynamically set the path to the class library c#.
For example, if the path to the DLL is not found.
Find DLL manually and specify the path where to load.
You can add a path to the app config that is searched for DLLs. Here's an example:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="C:\myDLLDirectory\" />
</assemblyBinding>
</runtime>
</configuration>
See here for more information and ideas. There are several ways to do it: http://support.microsoft.com/kb/837908

How to properly access the PrivateBinPath property of the current AppDomain?

Since AppDomain.AppendPrivatePath() is obsolete, I'm trying to figure out how to specify a PrivateBinPath for the current AppDomain in my project without spinning up a whole new AppDomain, and being able to access it later.
I know I can set the PrivateBinPath on an AppDomainSetup object (which would be ok if I wanted to create a new AppDomain), and I also know that I can add it to my app.config like so:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath=".\AlternateLookupPath" />
</assemblyBinding>
</runtime>
However, when adding this entry to my app.config, the AppDomain.CurrentDomain.SetupInformation.PrivateBinPath property is null.
use
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="AlternateLookupPath" />
</assemblyBinding>
</runtime>
According to http://msdn.microsoft.com/en-us/library/823z9h8w.aspx the privatePath is already interpreted as "subdirectories of the application's base directory"... so I suspect that using .\ is somehow messing things up...
From the docs:
If the directories specified for
PrivateBinPath are not under
ApplicationBase, they are ignored.
So, you need to make sure the paths you are add are under ApplicationBase.
This only works with app.config however. If you need to do this at runtime, use the AssemblyResolve event as described in the docs:
http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx

Categories