简体   繁体   中英

How to programmatically save Visual Studio solution?

I have an application that creates a visual studio solution programmatically and loads project and files that I created in VS2010. I would like to know how to automatically save the solution including project files that is not displaying the dialog box to save the solution file.

Code below:

Type type = Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
Object obj = System.Activator.CreateInstance(type, true);
EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)obj;
Solution2 soln = (Solution2)dte8Obj.Solution;
Solution2 soln2 = (Solution2)dte8Obj.Solution;
Project prj;
ProjectItem prjItem;

string prjPath = @"C:\SaveLocation\";
string prjName = "ProjectName";
soln.Create(prjName, prjName);
soln.DTE.MainWindow.Visible = true;
string csTemplatePath = soln2.GetProjectTemplate("WebApplicationProject40.zip", "CSharp");
soln.AddFromTemplate(csTemplatePath, prjPath + prjName, prjName, false);
prj = soln.Projects.Item(1);

save & asking for a file name and quit... For here I would like to save it automatcially.

dte8Obj.ExecuteCommand("File.SaveAll");
dte8Obj.Quit();

Just converting the above VB.Net Code to C# Code that credited & made by @spgennard

private void SaveAllFiles()
{
    for (int i = 1; i <= soln.Projects.Count; i++)
    {
        if (!soln.Projects.Item(i).Saved)
        {
            soln.Projects.Item(i).Save();
        }
        for (int j = 1; j <= soln.Projects.Item(i).ProjectItems.Count; j++)
        {
            if (!soln.Projects.Item(i).ProjectItems.Item(j).Saved)
            {
                soln.Projects.Item(i).ProjectItems.Item(j).Save();
            }
        }
    }
}

Try this:

Sub SaveAllFiles()
    For i = 1 To DTE.Solution.Projects.Count
        If Not DTE.Solution.Projects.Item(i).Saved Then
            DTE.Solution.Projects.Item(i).Save()
        End If
        For j = 1 To DTE.Solution.Projects.Item(i).ProjectItems.Count
            If Not DTE.Solution.Projects.Item(i).ProjectItems.Item(j).Saved Then
                DTE.Solution.Projects.Item(i).ProjectItems.Item(j).Save()
            End If
        Next
    Next 
End Sub

This will save the whole solution, all the projects within the solution and all the project files and items:

// Get EnvDTE / DTE.
DTE dte = (DTE) GetService(typeof(DTE));

// Get the current solution.
var solution = dte.Solution;

// Save the solution file.
if(!solution.Saved)
    solution.SaveAs(solution.FullName);

// Save the project files within the solution.
for(int i = 1; i <= solution.Projects.Count; i++)
{
    var project = solution.Projects.Item(i);

    // Check if this item is Solution Folder.
    if (project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
                continue;

    if (!project.Saved)
        project.Save();

    // Save all the files and items within the project.
    for(int j = 1; j <= project.ProjectItems.Count; j++)
    {
        var item = project.ProjectItems.Item(j);
        if (!item.Saved)
            item.Save();
    }
}

Notes

  • To save the solution, we can also use SaveAs method to overwrite the current solution:
  • You need to get an instance of DTE . Here I am getting in within the Microsoft.VisualStudio.Shell.Package class in my VSPackage.
  • Important : The Guid {66A26720-8FB5-11D2-AA7E-00C04F688DDE} indicates the Project Type of a Solution Folder. See this link to see the Guids for other project types. If you don't check for the Solution Folders and try to save it, Visual Studio will crash without throwing any exceptions. (have tested this on Visual Studio 2015 on multiple setups.) This Guid may change in future versions of Visual Studio.

EnvDTE is one alternative indeed, but it's fully bound to Visual studio model. Suspect it's quite old, as almost all objects are COM based, or C# does not provide good api access to EnvDTE classes. Anyway, not easy to use.

On my own I have developed syncProj command line utility, and one of API, which I have recently introduced, supports loading and saving solution files. At the moment not all features of solution are supported - for example platform mapping (Eg x64 to AnyCPU), as well as source code connection are not loaded or saved.

But API/tool is MIT licensed, so feel free to stick your hands on it and help me do it better.

Here is one example for loading .sln, modifying it and saving back:

https://github.com/tapika/syncProj/blob/master/tests/SlnToSln/TestChangeSolution.cs

Documentation of tool can be found from root readme / follow link.

(If you manage to find similar example using EnvDTE API's, please send me a link ! )

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