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?
是否可以使用c#在VS2005中以编程方式或动态创建项目?如果是这样,有人可以提供一些想法或链接,如何实现这一目标?
7 个解决方案
#1
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
您还可以使用msbuild api以编程方式创建和编辑msbuild文件。详情请见: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:
这是一个创建项目的示例,该项目编译单个.cs文件并引用外部程序集:
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
.csproj files are actually XML documents that represent msbuild executions.
.csproj文件实际上是表示msbuild执行的XML文档。
The files can be generated using System.xml, and if you need to validate it there's a schema detailed here.
可以使用System.xml生成文件,如果需要验证,可以在此处详细说明架构。
#3
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.
Visual Studio Projects是“简单”的XML文件。我之所以说,只是因为他们中有很多事情发生。要了解它们的组成:创建一个空项目并在文本编辑器中打开csproj(C#.NET项目)或vsproj(VB.NET项目)。
You could then generate this XML file in via code.
然后,您可以在via代码中生成此XML文件。
#4
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.
如果您希望能够一遍又一遍地创建相同的东西,Visual Studio支持模板。它甚至具有在从模板创建文件时扩展的宏。例如,当创建基于模板的文件时,$ username $将被登录用户的名称替换。
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.
如果那不是你想要做的,那么你可能会使用Visual Studio可扩展性来控制项目的创建。
#5
Take a look at the source code of this CodePlex project: STSDev
看看这个CodePlex项目的源代码:STSDev
It ends up being a WinForm app that dynamically produces a Visual Studio Solution with a Project.
它最终成为一个WinForm应用程序,动态生成带有项目的Visual Studio解决方案。
It is geared for Sharepoint, but the idea it uses is what you are looking for.
它适用于Sharepoint,但它使用的想法是你正在寻找的。
Keith
#6
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.
可以创建Visual Studio项目模板,允许在其中创建子项目。当您定义在项目创建期间运行的子项目模板时,这不会是真正动态的。
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
以编程方式创建csproj / vbproj文件以及所需的所有项目。这将包括在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)
创建项目模板并通过VS项目创建机制来调用创建项目方法。这可能非常棘手,因为我在它上面找到了0个文档,并且正在学习如何通过MS ASP.NET MVC项目创建向导DLL的逆向工程来实现它(因为它将动态地创建一个单元测试项目)
#7
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
对于Visual Studio 2017,我找到了一个基于envdte的解决方案。你不需要xml操作,它使用visual studio ide。你可以在这里找到Nager.TemplateBuilder项目
#1
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
您还可以使用msbuild api以编程方式创建和编辑msbuild文件。详情请见: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:
这是一个创建项目的示例,该项目编译单个.cs文件并引用外部程序集:
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
.csproj files are actually XML documents that represent msbuild executions.
.csproj文件实际上是表示msbuild执行的XML文档。
The files can be generated using System.xml, and if you need to validate it there's a schema detailed here.
可以使用System.xml生成文件,如果需要验证,可以在此处详细说明架构。
#3
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.
Visual Studio Projects是“简单”的XML文件。我之所以说,只是因为他们中有很多事情发生。要了解它们的组成:创建一个空项目并在文本编辑器中打开csproj(C#.NET项目)或vsproj(VB.NET项目)。
You could then generate this XML file in via code.
然后,您可以在via代码中生成此XML文件。
#4
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.
如果您希望能够一遍又一遍地创建相同的东西,Visual Studio支持模板。它甚至具有在从模板创建文件时扩展的宏。例如,当创建基于模板的文件时,$ username $将被登录用户的名称替换。
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.
如果那不是你想要做的,那么你可能会使用Visual Studio可扩展性来控制项目的创建。
#5
Take a look at the source code of this CodePlex project: STSDev
看看这个CodePlex项目的源代码:STSDev
It ends up being a WinForm app that dynamically produces a Visual Studio Solution with a Project.
它最终成为一个WinForm应用程序,动态生成带有项目的Visual Studio解决方案。
It is geared for Sharepoint, but the idea it uses is what you are looking for.
它适用于Sharepoint,但它使用的想法是你正在寻找的。
Keith
#6
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.
可以创建Visual Studio项目模板,允许在其中创建子项目。当您定义在项目创建期间运行的子项目模板时,这不会是真正动态的。
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
以编程方式创建csproj / vbproj文件以及所需的所有项目。这将包括在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)
创建项目模板并通过VS项目创建机制来调用创建项目方法。这可能非常棘手,因为我在它上面找到了0个文档,并且正在学习如何通过MS ASP.NET MVC项目创建向导DLL的逆向工程来实现它(因为它将动态地创建一个单元测试项目)
#7
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
对于Visual Studio 2017,我找到了一个基于envdte的解决方案。你不需要xml操作,它使用visual studio ide。你可以在这里找到Nager.TemplateBuilder项目