如何以编程方式创建 Visual Studio 解决方案 (.sln) 文件,包括网站项目?
我正在开发一个代码生成器项目,该项目创建具有可自定义层的解决方案。 现在我可以通过在代码中编写所有元素来创建 .sln 文件。但项目文件不需要这个,可以使用 MSBuild Project 类来编辑它们。
我想添加网站项目模板支持等,所以这种方式编辑 .sln 文件并不酷,我想知道是否有更好的方法来做到这一点,例如 MSBuild 或其他方法?
我看到 以编程方式生成 Visual Studio 解决方案 说要使用 Visual Studio SDK(用于扩展 Visual Studio、编写插件...),但没有任何代码示例。
提前致谢
I am working on a code generator project that creates solution with customizable layers.
for now I am able to create the .sln file by writing all the elements in code. but the project files don't need this, they can be edited using MSBuild Project class.
I want to add a Website project template support and etc, so this way I edit the .sln file is not cool, I wanted to know that is there a better way to do this, like MSBuild or something else ?
I saw Programmatically generate Visual Studio Solution that says to use Visual Studio SDK (which is for extending visual studio, writing plugins ...), but there isn't any code sample.
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里介绍如何使用SLNTools在代码中创建sln文件。代码很乱!但它有效!
SLN Tools 项目能够从项目文件创建 sln 文件。它是开源的,并提供用于编辑和合并 sln 文件的 UI。在这种情况下,您可以使用核心项目,并通过在项目中引用“CWDev.SLNTools.Core.dll”,您可以编写如下所示的混乱(!)代码:
注意:类的某些部分此处未显示,还有我自己的项目的一些自定义对象,如 Sln、Project 等,不应与同名的 SLNTools 对象混淆。需要的地方我都评论了!
希望有帮助。
Here is how to use SLNTools to create sln file in code. The code is a mess! But it works!
SLN Tools project is able to create sln file from project files. It's open source and it provides UI for editing and merging sln files. In this case you can use the core project and by referencing the 'CWDev.SLNTools.Core.dll' in your project you can write a messy(!) code like this:
NOTE: Some parts of class are not shown here and also There's some custom objects of my own project like Sln, Project and ... that should not be mistaken with SLNTools Objects with same name. I commented whereever needed!
Hope it helps.
您可以使用 IVsSolution 界面来创建解决方案并向其中添加项目。您将 SVsSolution 类型传递给GetService()方法来获取该接口的实例,如下所示:
You can use the IVsSolution interface to create solutions and add projects to them. You pass in the SVsSolution type to the GetService() method to get an instance of that interface like so:
在我的解决方案文件中,网站项目存储在 WebsiteProperties 项目部分中。
根据构建配置,这些属性将作为输入参数传递到 aspnet_compiler。尝试在包含网站项目的 sln 文件中搜索 projectID (GUID)(在本例中为 C9683FA5-AABA-497F-B780-80EFC3EB81F2),sln 文件的哪些部分应由代码生成器更新/生成。
您应该更新(至少)ProjectConfigurationPlatforms & NestedProjects 全局部分。在我的例子中这是需要的。
我已经实现了合并解决方案 MsBuild 任务。它支持将多个解决方案文件合并到一个 MergedSolution.sln 文件中。它适用于我们的 20 多个解决方案文件,其中包含 500 多个项目(c# 和网站以及 sql db 项目文件/部分)。输出解决方案文件大小为 1.3MB :)
编辑:
SLN Tools 项目 能够从项目文件创建 sln 文件。
如何使用自定义 MSBuild 任务生成 SLN 文件:
通过 自定义 MSBuild 任务(在 Execute 方法内)
In my solution files, web site projects are stored in the WebsiteProperties project section.
These properties are passed into the aspnet_compiler as input parameters depending on the build configuration. Try to search for projectID (GUID) (C9683FA5-AABA-497F-B780-80EFC3EB81F2 in this case) inside your sln files containing website project, what parts of sln file should be updated/generated by your code generator.
You should update (at least) ProjectConfigurationPlatforms & NestedProjects global sections. This was needed in my case.
I have implemented merge solution MsBuild task. It supports merging of multiple solution files into the one MergedSolution.sln file. It works in on our 20+ solution files containing 500+ projects (c# & website & sql db project files/sections). Output solution file size is 1.3MB :)
EDIT:
SLN Tools project is able to create sln file from project files.
How to generate SLN file with custom MSBuild task:
by custom MSBuild task (inside Execute method)