Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

c# Visual Studio ...adding references programmatically

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. Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...