Could not load file or assembly (run-time error) - c#

I placed a dll file in the bin folder of a class library project and added a reference to it. Added namespace but when I am creating object of a class of this dll it is giving me run time error -
Could not load file or assembly 'dll name' or one of its dependencies. The system cannot find the file specified.
I tried using the below code to load the dll:
Assembly MyDALL = Assembly.LoadFile("Test.dll");
Type MyLoadClass = MyDALL.GetType("Test.Class1");
object obj = Activator.CreateInstance(MyLoadClass);
Now it looks for its dependent dll in given error again.
Any solution to this?

You should try to load the .dll from the directory you are running your application in. You can get that from code using:
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
Note that if you put your .dll in the /bin directory, it might get overwritten next time you compile your application.
You might be better off including it into the project, and then marking it for output by right clicking it in the Solution Explorer, selecting Properties, and setting Copy to Output Directory to Copy Always or Copy if newer.
That way it will be published to your /bin directory automatically.

Related

Visual studio: Build action=Content causes error for dll file

I am trying to build a project in C# which uses some unmanaged dll files (MSVCRT.DLL, DFORRT.DLL, ...). There is no direct reference to the MSVCRT.DLL file in the project(this file is referenced by another dll and not directly by my program)
I want this dlls to be copied to bin directory. if i set build action to Content i get this error on run time:
The procedure entry point _wcstoui64 could not be located in dynamic link library msvcrt.dll.
Which is weird. I cant use other build actions because i want my dlls to be published too(Build action=None wont cause any error but doesn't let me to publish my dlls)
If i completely remove all references to all the dlls and treat them like data files, or pictures(files that are not used in code but should be copied to the output) again i get the same error.
What is causing the problem? how can i publish my dlls?

double clicking on file is not able to locate path

I have an app.config file where in I have a section for specifying the path of file to load it using reflection. Luckily assembly to be loaded and application .exe both are at same place. therefore, I have added following section in app.config
<pluing name = "xyz" path = "1P.SlotAssignment">
// path is simply name of assembly as it is located in the same folder where .exe is.
It seems to work fine when I open .exe from shortcut. But if I double click a file(file which is created and saved at different location via my .exe) to open in my application, then it crashes.
Because in this case, the path it looks is where file is located. It is not searching where .exe of application is located(~programfiles).
So, How to configure the path in correct way.
A Simple solution generate your assembly dynamically path by concating
System.Reflection.Assembly.GetEntryAssembly().Location;
and path from:
If you share your assembly load code i can help with that

Getting error "Unable to load DLL 'SQLite.Interop.dll'" despite it being in the bin folder

I have a ASP.Net Web Project and used NuGet to include System.Data.SQLite.
That shows under my references but when I publish my app (via "File System") and go to the controller "http://localhost/api/test", it shows the error:
Unable to load DLL 'SQLite.Interop.dll' The specified module could not be found.
I noticed, in my project's bin\Release\net452\win7-x86\x64 the DLL is there, but in the website's actual bin folder, the interop dll is not there!
How do i get it to be included? (I tried adding it as a reference and it wouldn't let me)
Find the location of the interop.dll and add it to the project, then select copy always. This will force the publishing profile to copy it to your deployment.

Could not load file or assembly, newtonsoft.json

I have a c# console application that is using JSON and it works fine when run on my development machine. When i try to move the application to another machine to run it i get the following exception when it comes to the part of my code which requires json:
Could not load file or assembly, newtonsoft.json 4.5.0.0
I presume i need to download/install this on the machine i want to run the application on. How would i go about doing that?
Download the Newtonsoft.Json.dll from http://json.codeplex.com/releases/view/107620 and place it in application's BIN folder.
Locate the missing assembly in the solution explorer (under references), right-click and select properties, then select
copy local: true
and the dll will be copied to your output folder and should be distributed with your program.

How to find out where my error is coming from?

I have an annoying error that's been popping up, and I don't know where it is coming from. The error is:
Error 31 Unable to copy file "app.config" to "bin\Debug\Framework.Tests.dll.config". Could not find file 'app.config'. Framework.Tests
The problem is, I don't have a bin\Debug folder anywhere, and it does not say from where it is trying to copy app.config. Double clicking on the error doesn't bring me to any code where it is trying to copy, so that doesn't help me either. So I don't know where I should make the app.config.
How can I find this out?
You have added a reference to a file called app.config or Visual Studio did this for you. The file does not exist on the disk, presumably because after adding the reference you later deleted the file.
Search for the missing file in your Solution Explorer, and either delete the reference or create the file.
It is most likely in the root folder of one of your projects, and should have a yellow triangle warning icon showing that the file is missing.
In an MSTest project the app.config is the one that you would like to provide any calls to the ConfigurationManager class.
It resides in the root of your test project.
The bin\debug folders will be created once the project compiles for the first time. Show all files in the solution explorer should help as they are not (and should not) be included in the project.
HTH
You probably do have a bin\Debug folder beneath your project folder, being the build target folder created by Visual Studio when you build your project for the Debug configuration.
My guess is that something (a test framework perhaps) still has the DLL file loaded, so Visual Studio can't delete and replace the existing Framework.Tests.dll.config file with the contents of your app.config. [Note: the project build action for app.config files is to copy it to the target folder renamed to match the executable with an extension of .config appended.]

Categories

Resources