Add project's reference to another project in same solution dynamically
Asked Answered
N

1

11

I have a solution and three projects inside that which is all created dynamically. Now I have to add the first project's reference to the second and third one. When i tried the logic i'm able to build the project individually and when building the solution it throws me the "Reference could not be loaded properly" error. This happens when the .dll is added directly using theVSProject.References.Add() method.

So i tried of using the below code to which is posted as a solution to the above issue. But still i'm getting some error.

Imports VSLangProj
' Add the second project as a reference to the first project.
Sub AddProjectExample()
   ' First project is a Visual Basic or C# project.
   Dim theVSProject As VSProject = _
      CType(DTE.Solution.Projects.Item(1).Object, VSProject)

   ' Second project is any type of project.
   Dim secondProject As Project = DTE.Solution.Projects.Item(2)

   ' AddProject returns the newly created Reference object.
   Dim newReference As Reference
   newReference = theVSProject.References.AddProject(secondProject)
End Sub

The error is,

The referenced assembly 'EnvDTE, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' could not be found. 

Any solution for this?

We don't have permission for VS IDE folder for applying the below fix.

Note: Have also tried the below solution for application App.Config but still there is issue.

msdn

Nightgown answered 11/3, 2014 at 9:30 Comment(10)
Could you programmatically modify the project file?Corrinnecorrival
I wonder if you could add another <ProjectReference ...> element to the appropriate <ItemGroup> element of the project file. You can include the path to the compiled .dll, the project GUID, and its name. <ProjectReference Include="..[project][projectName].csproj"> <Project>{[projectGuid]}</Project> <Name>[projectName]</Name> </ProjectReference>Corrinnecorrival
@Bone: Read the question first.Nightgown
@NWard: I have coded like using the ENVDTE, VSProject and References. Is there any idea to make the above code work.?Nightgown
Not sure I'm afraid, I don't have any experience with EnvDTE. I was just speculating if there could be another solution to your problem which doesn't involve EnvDTE. Can you tell us more about what problem you are trying to solve?Corrinnecorrival
Have you added the App.Config to the project being created or the project creating the projects? The latter should help.Palacio
Can you try adding another project with a type of Class Library? Then make the projects reference that.Persis
@jeffery: Please understand the question first. The issue is how to add the project reference and the admin privileges has nothing to do with this.Nightgown
Maybe you could give some background info on the problem you are trying to solve with this?Klein
EnvDTE version 7 is a pretty old one (Visual Studio 2005 I believe). Do you have that DLL around? Otherwise you may need a binding redirect, like in the sample here: msdn.microsoft.com/en-us/library/ms228768.aspxMinus
J
3

NWard is absolutely right. Not sure about the code you're trying to use to add the references but the csproj file in the project root can be edited programmatically to add the following in its rightful place:

<ItemGroup>
    <ProjectReference Include="..\MyOtherProj\MyOtherProj.csproj">
      <Project>{abda85bb-2019-4b80-982e-b90add3fcca0}</Project>
      <Name>MyOtherProj</Name>
    </ProjectReference>
  </ItemGroup>

Call an internally invoked powershell script or some console exe. Hope this helps!

Jupiter answered 18/3, 2014 at 20:30 Comment(2)
I have done this working with the .csproj file using XML. But i need to know whether it can be added without using the .csproj file.Nightgown
That was not mentioned in the question though, as a matter of fact, Visual Studio stores project references in the csproj file (AFAIK). See if this link can help you with a related insight: whathecode.wordpress.com/2012/10/07/…Jupiter

© 2022 - 2024 — McMap. All rights reserved.