1

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?

1
  • I'm curious as to your usage for such a task? Commented Dec 30, 2008 at 20:17

7 Answers 7

2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

Engine engine=new Engine(); this line is not working on VS 2012
1

.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.

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

0

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.