c# Visual Studio …adding references programmatically

前端 未结 3 1883
生来不讨喜
生来不讨喜 2020-12-01 14:30

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

3条回答
  •  粉色の甜心
    2020-12-01 15:23

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

提交回复
热议问题