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
Related
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.
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.
I have a visual studio project with is running absolutely fine.
But a new client requirement comes up for deployment for placing the different dlls in different folders.
We have a framework dll which can be used in a different project. There are some third-party dlls on which this framework dll depends upon. So when I use this dll from my project every dependent dll is copied to my local on the build as CopyLocal property is true.
But now with new requirement we can not have CopyLocal property set as True. The client wants no local copy of any dll, rather he wants framework related dll in some location. When I am doing this the dependent DLL's are not getting loded.
I know I have two options:
I can put them in GAC, but I don't want to do this as I want them to support xcopy.
Using reflection(But I am not sure of this that is this the right approach)
Can we do anything using configurations??
You can configure assembly probing paths using the <probing> configuration element:
Specifies application base subdirectories for the common language runtime to search when loading assemblies.
Example from MSDN:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>
However, if the assemblies in question reside outside the application base ("which is the root location where the application is being executed"), you have the <codeBase> configuration element:
Specifies where the common language runtime can find an assembly.
Example from MSDN:
<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>
For the exact details of how the runtime locates assemblies you can refer to this MSDN article.
As OP pointed out, unfortunately codeBase element is a usable option for strong named assemblies only. For private assemblies you need a workaround. Some viable ideas can be found in this discussion such as:
file system links (NTFS junction point) + probing element or AppDomainSetup.PrivateBinPath
AppDomain.CurrentDomain.AssemblyResolve event
I've tested the latter and can confirm it works:
AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
Assembly.LoadFile(Path.Combine(Settings.Default.AssemblyPath, Path.ChangeExtension(e.Name.Substring(0, e.Name.IndexOf(',')), ".dll")));
In my project I have to use different versions of AWSSDK dll's, in order to make this i took help of this post . and added one of my dll in to a folder named V-1 inside bin folder. Then made config changes like this
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="AWSSDK" publicKeyToken="CD2D24CD2BACE800" culture="neutral" />
<codeBase version="1.4.8.2" href="V-1\AWSSDK.dll" />
<codeBase version="2.3.40.0" href="AWSSDK.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
But I still gets the error like this
Error 2 Could not load file or assembly 'AWSSDK, Version=1.4.8.2, Culture=neutral, PublicKeyToken=cd2d24cd2bace800' or one of its dependencies. The system cannot find the file specified. E:\Live \Web.config 129
At this line in web-Config
<add assembly="AWSSDK, Version=1.4.8.2, Culture=neutral, PublicKeyToken=CD2D24CD2BACE800" />
Can anyone please point-out what I am doing wrong??
Please open Solution Explorer in visual studio
Open References under the project
Select AWSSdk reference and go to its properties.
Set Specific Version = True and Copy Local = False
Make sure your output directory does not contain this dll in it.
I have been trying to load a private assembly which is located in a subdirectory under the application base directory. I have an assembly named Sparrow.dll which is located under plugins directory (which is under application base dir also). Whenever I call Assembly.Load("Sparrow") I get a System.IO.FileNotFoundException.
I used app.exe.config with tag and it worked with a strong named version of the same assembly with the line below;
Assembly assem = Assembly.Load("Sparrow");
However, it does not work when I changed the assembly in to a weak assembly.
The content of the config file is below;
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly name="Sparrow, Culture=neutral, PublicKeyToken=xxxxxx">
<codeBase version="1.0.1.1" href="plugins/Sparrow.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I read many things, but I am not sure whether using tag for locating weak assemblies is a good practice or not.
You can use the probing element for that purpose:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="plugins" />
</assemblyBinding>
</runtime>
</configuration>
That element means that the plugins subfolder will be searched for assemblies.
Note however, that only directories that are on a descendent path of the application directory can be specified in that way.
The configuration file in your question has a mistake. According to documentation, the XML configuration should look like:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Sparrow"
publicKeyToken="null"
culture="neutral" />
<codeBase version="1.0.1.1" href="plugins/Sparrow.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
However, I think that using the probing element would be a better choice in your case.