简体   繁体   中英

Making a solution file without using visual studio

I'm building a program which generates a lot of code files (cpp,h,hpp files). 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. 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.

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.

MSDN:

The Microsoft.Build namespaces contain types that provide programmatic access to, and control of, the MSBuild engine.

ProjectRootElement is the primary class used to add files to and create your solution progmatically.

Represents an MSBuild project, a targets file, or any other file that conforms to MSBuild project file schema. This class and its related classes allow a complete MSBuild project or targets file to be read and written. 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:

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() . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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