Could not load file or assembly in Visual Studio 2012, C#
Asked Answered
S

1

3

I’m working on a C# code on Visual Studio 2012 on Windows 7. My solution contains several projects.

I recently added a new project in my solution, and added the dll of this new project in the references of 2 other projects in the solution (one of the being the startup project). The new project itself is referencing to several other projects that are not included in my solution.

Building the projects in the solution works fine, but when I try to run the program (in Debug or in Release) I get an error. Here is the error:

System.IO.FileNotFoundException was caught
  HResult=-2147024894
  Message=Could not load file or assembly 'ABCD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  Source=NP
  FileName=ABCD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog==== Pre-bind state information ===
LOG: User = MyCompany\MyName
LOG: DisplayName = ABCD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Users/MyName/Documents/Visual Studio 2012/Projects/MyProgram/Analysis/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : NP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\MyName\Documents\Visual Studio 2012\Projects\MyProgram\Analysis\bin\Debug\Analysis.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/MyName/Documents/Visual Studio 2012/Projects/MyProgram/Analysis/bin/Debug/ABCD.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyName/Documents/Visual Studio 2012/Projects/MyProgram/Analysis/bin/Debug/ABCD/ABCD.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyName/Documents/Visual Studio 2012/Projects/MyProgram/Analysis/bin/Debug/ABCD.EXE.
LOG: Attempting download of new URL file:///C:/Users/MyName/Documents/Visual Studio 2012/Projects/MyProgram/Analysis/bin/Debug/ABCD/ABCD.EXE.

  StackTrace:
       at NewProject.DATA.SetType(NP np, String code, OrderType orderT, MODEL MODELv)
       at NewProject.DATA.SetData(NP np, String no, String type, String law, String time, OrderType orderT, MODEL MODELv)
  InnerException:

Analysis is my startup project. NewProject is the project I added recently to my solution. ABCD.dll is referenced in NewProject, but the project ABCD is not part of the solution itself.

In this error, it is said that the AppBase is file:///C:/Users/MyName/Documents/Visual Studio 2012/Projects/MyProgram/Analysis/bin/Debug/

However, when I look at the ABCD reference in NewProject, it says:

Assembly ABCD
    C:\Users\MyName\Documents\Visual Studio 2012\Projects\np\bin\ABCD.dll

Indeed, ABCD.dll in not in the AppBase address written in the error. But I never said it was there, and I don’t understand where this comes from.

I tried to put ABCD.dll in the AppBase address the error is giving me. But each time I rebuild NewProject and (then) my startup project Analysis, ABCD.dll disappears from C:/Users/MyName/Documents/Visual Studio 2012/Projects/MyProgram/Analysis/bin/Debug/

I am quite new to Visual Studio. I probably did something wrong when adding my new project but I can’t understand what it is. If one of you has an idea regarding the method to solve this issue, please don’t hesitate to speak !

Sargeant answered 27/8, 2013 at 13:36 Comment(0)
V
4

If you need references between projects, then add "Project references" instead of referencing DLL files. If you really need to reference a DLL file, then:

1) Create a "Libraries" (or something) folder in your project.

2) Copy the referenced DLL into this folder.

3) In VS, use "Add existing item" to add the DLL file to the project. In the file choosing dialog box you need to change the file filter to *.* for the DLL to appear.

4) Under the properties of this DLL's node choose "Copy if newer" in Build option.

5) Add reference to the DLL file in the "Libraries" directory.

References between projects are of course better, because as your rebuild them your references are updated automatically. Otherwise you need to replace the DLL file with a fresh one each time you want to update the reference.

Vallombrosa answered 27/8, 2013 at 13:44 Comment(2)
For subversion reason I reference to NewProject with NP.dll Sorry, Andrew’s and your answers feel unclear. You said I should (I probably misunderstood because it makes no sense to me): 1)Create empty “Libraries” folder in the project NewProject 2)in which I copy ABCD.dll and the other DLLs needed for NewProject. 3)? Not sure what DLL or project you are talking about. Anyway I can’t use “Add existing item” to add a DLL file (the dll files don’t appear). What I did is NewProject > Add Reference > ABCD.dll 4)“Copy if newer” option appears in File properties, but not in Reference propertiesSargeant
You add a DLL using "Add existing item" to the folder "Libraries" in your project. For DLL to appear change the filter in the bottom right corner to . or *.dll (by default it seeks only code files to add). After this you will be able to choose "Copy if newer option". This way your DLL will be copied along with the exe file allowing you to run the project.Vallombrosa

© 2022 - 2024 — McMap. All rights reserved.