0

Anyone knows how can I add a dependency reference programmatically in C#?

The think is I am already generating a new project programmatically, but I need to add some references to that project.

Is it possible to do it programmatically?

Thanks.

6
  • 1
    How about adding these dependencies to the .csproj? Commented Aug 14, 2013 at 15:32
  • Compile the project and load the dll? Commented Aug 14, 2013 at 15:32
  • Adding to the csproj file will be a good option. But there is another way? Commented Aug 14, 2013 at 15:34
  • 4
    This has already been answered here: stackoverflow.com/questions/4410258/… Commented Aug 14, 2013 at 15:35
  • @user2521713, adding them programmatically really means modifying the XML of the .csproj. That's what Visual Studio does. But you're not in the context of VS, so you just need to add them. Commented Aug 14, 2013 at 15:35

1 Answer 1

2

See Scott's answer here: c# Visual Studio ...adding references programmatically

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

Solution2 pSolution = (Solution2)pEnv.VS.Solution;
Project pProject = pSolution.Projects[0];
pProject.References.Add(string referenceFilePath);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.