Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Create a Visual Studio project programmatically

As my question say I want to create a new project based in a template which already created an tested and works fine, but i have two problems when i tried to do it in C# code (in a mvc3 project).

  1. Which are the differences between EnvDTE80, EnvDTE90 and EnvDTE100 because i tried to do this example with EnvDTE100 but it doesn't work because the object handle it's Solution4 not Solution2 and Solution4 doesn't have the same behavior.
  2. How can I create the project without use the default path, but an specific folder that i need

UPDATE

here's the code that works if I used the dll called EnvDTE80

  System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0");
  Object obj = System.Activator.CreateInstance(type, true);
  EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)obj;
  Solution2 _solution = (Solution2)dte.Solution;
  string projectTemplatePath = @"C:Documents and SettingsjmachadoEscritorio";
  projectTemplatePath =_solution.GetProjectTemplate("",""); <-- looking for some overload to create project based in a specific folder an not from '<drive>:Program FilesMicrosoft Visual Studio 8Common7IDEProjectTemplatesLanguage.'

But if i used the EnvDTE100

  System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
  Object obj = System.Activator.CreateInstance(type, true);
  EnvDTE100.DTE2 dte = (EnvDTE100.DTE2)obj;
  Solution4 _solution = (Solution4)dte.Solution;
  string projectTemplatePath = @"C:Documents and SettingsjmachadoEscritorio";
  projectTemplatePath =_solution.GetProjectTemplate("",""); <-- looking for some overload to create project based in a specific folder an not from '<drive>:Program FilesMicrosoft Visual Studio 8Common7IDEProjectTemplatesLanguage.'

and Say's that DTE2 doesn't exit's in the namespace of EnvDTE100

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

EnvDTE80, EnvDTE90 and EnvDTE100 are DTE type libraries for VS 8.0 (2005), 9.0 (2008) and 10.0 (2010), correspondingly.

There are only two DTE root object interfaces, as of VS2010 - DTE2 being the latest. So, to get the DTE object for VS 2010, you do:

System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
Object obj = System.Activator.CreateInstance(type, true);
EnvDTE80.DTE2 dte = (EnvDTE100.DTE2)obj;

Note that ProgID is for "10.0", but variable type is still EnvDTE80.DTE2.

The rest should work from there. Note also that you can always cast Solution4 to Solution2 if you need it (but GetProjectTemplate should be available directly on Solution4).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...