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?
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.
.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.
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")
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.
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
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)