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();
By default, VS Code requires an explicit action to save your changes to disk, Ctrl+S.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With