When I have the library in the same folder as app I can simply:
[DllImport("kernel32")]
public extern static IntPtr LoadLibrary(string librayName);
IntPtr iq_dll = LoadLibrary("IQPokyd.dll");
I also have this in my app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="plugins;" />
</assemblyBinding>
</runtime>
</configuration>
I'm able to load this library. Problem is that it is using some config files which needs to be in app run directory. Is there some possibility how to tell the library that files it needs are in the same folder as the library?
Since the DLL searches for the config in the current directory, it makes sense to temporarily change the current directory before loading it:
string saveCurDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Path.Combine(Application.StartupPath, "plugins"));
IntPtr iq_dll = LoadLibrary("IQPokyd.dll");
Directory.SetCurrentDirectory(saveCurDir);
You can do this by adding a probing tag to your app's .config file.
For example, if you wanted libraries to be loaded from a /lib/ subdirectory, your config would look like this:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib"/>
</assemblyBinding>
</runtime>
</configuration>
Related
I know this kind of question has been asked many times already, but for older versions of .NET and on 4.8 my project has been working fine with such in app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="languages;bin;"/>
</assemblyBinding>
</runtime>
...
</configuration>
but after I update to .NET 6 - its stops, anybody knows how to make it work on .NET6?
I've recently finished converting a WPF project to the new csproj format which is much leaner.
However I have one missing piece left, adding a probing path for the assemblies, something that used to exist in the old app.config file. With this missing my application just doesn't find the required dlls.
The way I have this set up is with a Post-build event that clears and moves items to a bin folder:
SET folder=bin
rmdir "$(TargetDir)%folder%" /s /q
mkdir "$(TargetDir)%folder%"
move "$(TargetDir)*.dll" "$(TargetDir)%folder%\"
I've tried adding some entries to the .csproj file but to no avail. I believe this is more for compiling the application:
<PropertyGroup>
<ReferencePath>bin</ReferencePath>
</PropertyGroup>
<PropertyGroup>
<AssemblySearchPaths>
$(AssemblySearchPaths);
$(ReferencePath);
</AssemblySearchPaths>
</PropertyGroup>
I guess my main question, should I still have an app.config, or is there a better .csproj approach available?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin"/>
</assemblyBinding>
</runtime>
</configuration>
My application will work if I include it, but I'd like to know if I can simplify this, or do this in a better way in general.
I have a large ASP.NET MVC project with a data access layer that uses Entity Framework. As well, I have a Windows service project (in a different solution) that makes use of the same DAL project. I have my Web.config/App.config setup so that the connection strings are loaded from a separate connection.config file that is in a shared location. The connection configuration is linked (Add Existing Item > Add As Link) in both projects with Build Action = Content and the Copy To Output Directory = Copy if newer.
The Windows service project runs with no issues, I can see the service connecting to my database correctly. However the ASP.NET MVC project throws an excpetion saying Unable to open configSource file 'connection.config'. (C:\Path\To\My\web.config line 9).
I checked that the connection.config file is copied to output directory. I also tried making a copy of the connection.config file in the ASP.NET project (instead of linking it) and this runs just fine. Why does linking the file suddenly cause an exception?
My files below for reference
App.config (Windows service)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connection.config" />
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<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>
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
[...]
<connectionStrings configSource="connection.config" />
[...]
</configuration>
connection.config
<?xml version="1.0"?>
<connectionStrings>
<clear />
<add name="MyConnection" connectionString="Data Source=./localhost;Initial Catalog=MyBatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
Edit
I came across this answer that works, however I'm concerned how this would affect the application in release mode or when it's deployed. As well I still don't understand why having the file linked has a different behavior than when it's copied in.
When building my WPF application all internationalization/locale folders are put inside the folder of the executable.
/MyApp
/MyApp/de
/MyApp/fr
/MyApp/otherSpecialFolder
/MyApp/...
The problem is that this mixes up with some other folders. Is it possible to put the internationalization into a separate folder and let the Wpf app search there instead? For example:
/MyApp
/MyApp/i18n/de
/MyApp/i18n/fr
/MyApp/otherSpecialFolder
/MyApp/...
This problem occurs not only for own localization ressources but also when adding thirdparty controls like Xceed.Wpf.AvalonDock.
Yes you can. You just have to add probing element into your App.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="i18n"/>
</assemblyBinding>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
probing element specifies the path where assemblies are searched for.
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