Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically save Visual Studio solution?

Tags:

c#

envdte

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();
like image 782
juan Avatar asked Aug 01 '14 16:08

juan


People also ask

How do I save code in Visual Studio?

By default, VS Code requires an explicit action to save your changes to disk, Ctrl+S.

How do I save a Visual Studio project?

You can click File, Close Project on the Menu Bar to close an open project – a dialog will prompt you to save any changes before closing.

Where are Visual Studio solutions saved?

When you create a new project, Visual Studio saves it to its default location, %USERPROFILE%\source\repos. To change this location, go to Tools > Options > Projects and Solutions > Locations. For more information, see Options dialog box: Projects and Solutions > Locations.


2 Answers

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();
            }
        }
    }
}
like image 51
juan Avatar answered Nov 15 '22 10:11

juan


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
like image 39
Stephen Gennard Avatar answered Nov 15 '22 12:11

Stephen Gennard