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?

0

3 Answers 3

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); 
Sign up to request clarification or add additional context in comments.

Comments

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"); } 

2 Comments

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.
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).
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 

11 Comments

Why the down vote?? It's exactly how you load plugins without using third party libraries. Assembly.Load and then Activator.CreateInstance
This will not add the reference to the solution, it will simply load an Assembly for the running code.
Some people are just weird! I've upvoted it- thanks for the response.
@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 - if it accomplishes the same job and solves the problem at hand, then what's the point of bickering over semantics?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.