1

是否可以使用 c# 在 VS2005 中以编程方式或动态创建项目?如果是这样,有人可以提供一些关于如何实现这一点的想法或链接吗?

4

7 回答 7

2

您还可以使用 msbuild api 以编程方式创建和编辑 msbuild 文件。有关详细信息,请参见此处:http: //msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project.aspx

下面是一个创建编译单个 .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")
于 2008-12-30T21:19:49.357 回答
1

.csproj 文件实际上是代表 msbuild 执行的 XML 文档。

这些文件可以使用 System.xml 生成,如果您需要验证它,这里有一个详细的架构

于 2008-12-30T20:22:50.627 回答
0

可以创建允许在其中创建子项目的 Visual Studio 项目模板。当您定义在项目创建期间要运行的子项目模板时,这不会是真正动态的。

要真正动态化,您需要执行以下任一操作:

  • 以编程方式创建 csproj/vbproj 文件以及您想要的所有项目。这将包括在 XML 中设置引用等

或者:

  • 创建项目模板并通过VS项目创建机制来调用create project方法。这可能非常棘手,因为我在上面找到了 0 个文档,并且正在学习如何通过从 MS ASP.NET MVC 项目创建向导 DLL 进行逆向工程来做到这一点(因为这将动态地创建一个单元测试项目)
于 2008-12-30T22:48:27.447 回答
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.

于 2008-12-30T21:53:47.977 回答
0

看一下这个 CodePlex 项目的源代码:STSDev

它最终成为一个 WinForm 应用程序,可以动态生成带有项目的 Visual Studio 解决方案。

它适用于 Sharepoint,但它使用的理念正是您所寻找的。

基思

于 2008-12-30T22:27:40.703 回答
0

Visual Studio 项目是“简单的”XML 文件。我这么说只是因为它们里面发生了很多事情。要了解它们的组成:创建一个空项目并在文本编辑器中打开 csproj(C#.NET 项目)或 vsproj(VB.NET 项目)。

然后,您可以通过代码生成此 XML 文件。

于 2008-12-30T20:22:25.963 回答
0

对于 Visual Studio 2017,我找到了基于 envdte 的解决方案。您不需要 xml 操作,它使用 Visual Studio IDE。你可以在这里找到项目Nager.TemplateBuilder

于 2017-07-31T13:41:31.733 回答