简体   繁体   English

在不使用Visual Studio的情况下制作解决方案文件

[英]Making a solution file without using visual studio

I'm building a program which generates a lot of code files (cpp,h,hpp files). 我正在构建一个程序,该程序会生成很多代码文件(cpp,h,hpp文件)。 using a "Generate" button triggers the making of the new files, I want to add a another functionality, I also want the "Generate" button to make a sulotion(for visual studio) after it done generating the files with the right settings, so the users wont have to deal with it. 使用“生成”按钮触发新文件的制作,我想添加其他功能,我还希望在通过正确的设置生成文件后,“生成”按钮可以制作出供视觉工作室使用的Sulotion(适用于Visual Studio),因此用户将不必处理它。 so the user clicks "Generate", new files are generated and he can also open the whole project in visual studio, fixed in the right order. 因此,用户单击“生成”,将生成新文件,并且他还可以在Visual Studio中打开整个项目,并以正确的顺序进行固定。

how can I do that? 我怎样才能做到这一点?

You can take a leaf out of the custom Visual Studio Project System examples and use the Microsoft.Build.Construction namespace of the Microsoft.Build.dll assembly. 您可以从自定义Visual Studio Project System示例中摘取叶子,并使用Microsoft.Build.dll程序集的Microsoft.Build.Construction命名空间。

MSDN: MSDN:

The Microsoft.Build namespaces contain types that provide programmatic access to, and control of, the MSBuild engine. Microsoft.Build命名空间包含提供对MSBuild引擎进行编程访问和控制的类型。

ProjectRootElement is the primary class used to add files to and create your solution progmatically. ProjectRootElement是用于向编程添加文件和创建解决方案的主要类。

Represents an MSBuild project, a targets file, or any other file that conforms to MSBuild project file schema. 表示一个MSBuild项目,一个目标文件或任何其他符合MSBuild项目文件架构的文件。 This class and its related classes allow a complete MSBuild project or targets file to be read and written. 该类及其相关类允许读取和写入完整的MSBuild项目或目标文件。 Tell me more... 告诉我更多...

For example, to start off your solution you might: 例如,要开始您的解决方案,您可以:

var project = ProjectRootElement.Create();

project.DefaultTargets = "Build";
project.ToolsVersion = "4.0";

...to add files you might do something like: ...要添加文件,您可以执行以下操作:

foreach (var unescapedFile in allFiles)
{
    var file = ProjectCollection.Escape(unescapedFile);
    var ext = Path.GetExtension(file);
    var fileType = "Content";   
    folders.Add(Path.GetDirectoryName(file));    
    var item = project.AddItem(fileType, file);
    .
    .
    .
}

...finalise the solution with: ...通过以下方式最终确定解决方案:

var imports = project.AddPropertyGroup();
imports.AddProperty("VisualStudioVersion", "10.0")
       .Condition = " '$(VisualStudioVersion)' == '' ";

(customization ?? DefaultProjectCustomization.Instance).Process(
                                                                project,
    new Dictionary<string, ProjectPropertyGroupElement>
    {
        {"Globals", globals},
        {"Imports", imports},
        {"Debug", debugGroup},
        {"Release", releaseGroup}
    }
    );

...and finally save the solution as XML with: ...最后通过以下方式将解决方案另存为XML:

project.Save(writer);

Templates 模板

EDIT : Kudos to millimoose for this suggestion in the comments below :) 编辑 :在下面的评论中对此建议表示感谢毫微粉 :)

If you want to load a "template" project file to be used a basis for new projects you may want to look into ProjectRootElement.TryOpen() . 如果要加载“模板”项目文件以用作新项目的基础,则可能需要查看ProjectRootElement.TryOpen() It will return a ProjectRootElement that you could possibly use to modify and/or add new items too, saving the result as a new project. 它将返回一个ProjectRootElement ,您也可以使用它来修改和/或添加新项目,将结果保存为新项目。

eg 例如

using (var writer = new StreamWriter("NewProject.csproj", false, Encoding.UTF8))
{
    var project = ProjectRootElement.TryOpen("my template.csproj");
    project.AddItem("Content", "miss piggy.png");
    project.Save(writer);
}

Tell me more 告诉我更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM