Dynamically creating a project in VS2005 using C #

Is it possible to build a project progmatically or dynamically in VS2005 using C #? If someone could provide some ideas or links on how to do this?

+1


source to share


7 replies


You can also create and edit msbuild files programmatically using the msbuild api. See here for more 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")

      

+2


source


Files

.csproj are actually XML documents that represent msbuild execution.



Files can be generated using System.xml, and if you need to check it there, then here it is .

+1


source


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

Then you can generate this XML file using code.

0


source


If you want to be able to create the same things over and over, Visual Studio supports templates. It even has macros that expand when the file is created from a template. For example, $ username $ will be replaced with the name of the logged in user when the file is generated from the template.

If this is not what you are trying to do, then you can use Visual Studio's extensibility to control project creation.

0


source


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

Ultimately it's a WinForm application that dynamically builds a Visual Studio solution with a project.

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

Whale

0


source


Visual Studio project templates can be created, allowing you to create child projects within them. This will not be really dynamic as you decide which child project templates to run at the time of project creation.

To be truly dynamic, you need to do one of the following:

  • Programmatically create a csproj / vbproj file along with all the required elements. This will include linking, etc. In XML

Or:

  • Create a project template and download through the VS project creation mechanisms to call the project creation method. It can be very tricky as I found 0 documentation on the subject and learned how to do it by reverse engineering from MS Project MVC Project create master DLL (as this will dynamically create Unit Test project)
0


source


For Visual Studio 2017, I found an envdte based solution. You don't need to manipulate the xml, it uses visual studio ideal. You can find the project here Nager.TemplateBuilder

0


source







All Articles