17

Is there anyway that a reference can be added to a solution programmatically?

I have an add-in button, when the user presses it, I want a reference to be added.

The reason is, I have created a piece of software that I want to be integrated into any given VS program (if the developer wants it), they would simply click the add-in button and the reference would be loaded in the current solution.

Is this possible?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Darren Young
  • 10,972
  • 36
  • 91
  • 150

3 Answers3

7

Something like this I haven't tested it

get the environment

EnvDTE80.DTE2 pEnv = null;
Type myType = Type.GetTypeFromProgID("VisualStudio.DTE.8.0");          
pEnv = (EnvDTE80.DTE2)Activator.CreateInstance(myType, true);

get the solution.

Solution2 pSolution = (Solution2)pEnv.VS.Solution;

get the project that you want

Project pProject = pSolution.Projects[0];

add the reference

pProject.References.Add(string referenceFilePath);
scott
  • 2,991
  • 5
  • 36
  • 47
3

There is an example on CodeProject.

The functionality is contained within a single class elRefManager and the method to call is CheckReferences. The code can be looked at here by selecting the elRefManager.cs file on the left hand side.

As seen in the article you could do...

private void button1_Click(object sender, System.EventArgs e)
{
    int ec;
    ec=elRefManager.CheckReferences(null, new string[] {textBox1.Text});

    if (ec<0)
        MessageBox.Show("An error occurred adding this reference");
    if (ec>0)
        MessageBox.Show("Could not add " + textBox1.Text + 
                    "\nCheck its spelling and try again");
}
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • This is directly related to what Darren is asking for. He is asking how to add a reference to a project programatically from an *add-in* meaning he has access to the DEV instance/solution/project system. He is *not* asking how to load an assembly dynamically at run time. – casperOne Dec 10 '10 at 15:34
  • I will say you should indicate where in the article Darren can look at the code to figure out how to integrate it into his add-in (this is an external program which accesses the running object table and then interfaces with the same interfaces an add-in would). – casperOne Dec 10 '10 at 15:35
1

System.Assembly.load Allows you to call functions in a library that were not built with your program.


If you want to add a reference to the project so that its in the solution you can use the following. Basically the same as @Scots answer.

I did it in a macro which is vb but I'm sure you can get the idea

    DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
    Dim objProject As EnvDTE.Project
    Dim i As Long
    i = DTE.Solution.Projects.Count
    For Each objProject In DTE.Solution.Projects
        If (objProject.Name() = "csCA") Then
            Dim vsproj As VSLangProj.VSProject
            vsproj = objProject.Object
            vsproj.References.Add("C:\Users\test.dll")
        End If
    Next
rerun
  • 25,014
  • 6
  • 48
  • 78
  • Why the down vote?? It's ***exactly*** how you load plugins without using third party libraries. `Assembly.Load` and then `Activator.CreateInstance` – jgauffin Dec 10 '10 at 15:22
  • 3
    This will not add the reference to the solution, it will simply load an `Assembly` for the running code. – Daniel DiPaolo Dec 10 '10 at 15:22
  • Some people are just weird! I've upvoted it- thanks for the response. – Darren Young Dec 10 '10 at 15:23
  • Daniel DiPaulo: What do you think that adding references at runtime will do (when it comes to addins)? – jgauffin Dec 10 '10 at 15:29
  • 1
    @jgauffin: Adding a reference and loading an assembly are two *completely* different things. Darren is asking how to add a reference, not load an assembly. – casperOne Dec 10 '10 at 15:36
  • Adding a reference is simply telling the complier to do this for you when the file is built. – rerun Dec 10 '10 at 15:37
  • @rerun: Yes, that's if you are using the compiler from the command line. If you are using the project system/MSBUILD, it means that you have to update the project file as well. Either way, it doesn't involve System.Assembly.Load at all. – casperOne Dec 10 '10 at 15:56
  • 1
    @casperOne - if it accomplishes the same job and solves the problem at hand, then what's the point of bickering over semantics? – mellamokb Dec 10 '10 at 17:08
  • I originally took the question as how do I in effect add a reference to a file that was already built. Yes I agree it will not add the project to the solution. Which is why I went and wrote a macro to do so. – rerun Dec 10 '10 at 17:10
  • @mellamokb: That's the problem, the results are *not* the same, so semantics are irrelevant here. The question refers to an add in which is loaded into VS, and wants the add in to add a reference to the project which is being developed. This *is not* something that is happening at run time. – casperOne Dec 10 '10 at 17:40
  • @mellamokb: Check the third comment on the question itself, Darren indicates that is exactly what he wants (to add a reference to a project when his add-in, loaded in VS has a button clicked on it). – casperOne Dec 10 '10 at 17:41