LostToThis
LostToThis

Reputation:

Dynamically creating a project in VS2005 using C#

Is it possible to progmatically or dynamically create a project in VS2005 using c#? If so could someone provide some ideas or links on how to accomplish this?

Upvotes: 1

Views: 1934

Answers (7)

live2
live2

Reputation: 4265

For Visual Studio 2017 i have found a solution based on envdte. You dont need xml manipulation, its use the visual studio ide. You can found the project here Nager.TemplateBuilder

Upvotes: 0

rnwood
rnwood

Reputation: 1480

You can also create and edit msbuild files programatically using the msbuild api. See here for details: http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project.aspx

Here's an example that creates a project that compiles a single .cs file and references an external assembly:

Engine engine=new Engine();
engine.BinPath=RuntimeEnvironment.GetRuntimeDirectory();
Project project=engine.CreateNewProject();

project.AddNewImport(@"$(MSBuildBinPath)\Microsoft.CSharp.targets", null);

BuildPropertyGroup props=project.AddNewPropertyGroup(false);
props.AddNewProperty("AssemblyName", "myassembly");
props.AddNewProperty("OutputType", "Library");

BuildItemGroup items=project.AddNewItemGroup();
items.AddNewItem("Reference", "Some.Assembly");
items.AddNewItem("Compile", "somefile.cs");

project.Save("myproject.csproj")

Upvotes: 2

Aaron Powell
Aaron Powell

Reputation: 25099

Visual Studio project templates can be created which allow child projects to be created within them. This wont be truely dynamic as you define what child project templates to run during the project creation.

To be truely dynamic you need to do either of the following:

  • Programatically create the csproj/ vbproj file along with all items you want in there. This would include setting references, etc in the XML

Or:

  • Create a project template and hock in through the VS project creation mechanisums to call the create project method. This can be very tricky as I found 0 documentation on it and was learning how to do it by reverse engineering from the MS ASP.NET MVC project create wizard DLL (as that will dynaically create a Unit Test project)

Upvotes: 0

Keith Sirmons
Keith Sirmons

Reputation: 8411

Take a look at the source code of this CodePlex project: STSDev

It ends up being a WinForm app that dynamically produces a Visual Studio Solution with a Project.

It is geared for Sharepoint, but the idea it uses is what you are looking for.

Keith

Upvotes: 0

Slapout
Slapout

Reputation: 3809

If your wanting to be able to create the same things over and over again, Visual Studio supports templates. It even has macros that expand when a file is created from the template. For instance, $username$ would be replaced by the name of the logged in user when a file based on the template was created.

If that's not what you're trying to do, then you might be about to use the Visual Studio extensibility to control the creating of projects.

Upvotes: 0

Dan Monego
Dan Monego

Reputation: 10087

.csproj files are actually XML documents that represent msbuild executions.

The files can be generated using System.xml, and if you need to validate it there's a schema detailed here.

Upvotes: 1

Gavin Miller
Gavin Miller

Reputation: 43815

Visual Studio Projects are "simply" XML files. I say simply because there is a lot going on in them. To get an idea of what they consist of: Create an empty project and open up the csproj(C#.NET project) or vsproj (VB.NET project) in a text editor.

You could then generate this XML file in via code.

Upvotes: 0

Related Questions