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

独自空忆成欢 提交于 2019-12-10 20:11:48

问题


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 !


回答1:


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.



来源:https://stackoverflow.com/questions/18467049/could-not-load-file-or-assembly-in-visual-studio-2012-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!