views:

474

answers:

7

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?

A: 

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.

Gavin Miller
+1  A: 

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

Dan Monego
+1  A: 

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.Assmebly");
items.AddNewItem("Compile", "somefile.cs");

project.Save("myproject.csproj")
Robert Wood
A: 

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.

Slapout
A: 

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

Keith Sirmons
A: 

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)
Slace
A: 

Hi

have a look at Codus, the 1.3.2 the source code is downloadable

Codus

It creates business objects from the DB, it does it by opening Visual Studio and adding in the files, you can see it happen. (creates the project and files)

HTH

bones

dbones