This documentation is archived and is not being maintained.

Project.Save Method

Saves the project or project item.

Namespace: EnvDTE
Assembly: EnvDTE (in envdte.dll)

void Save (
	[OptionalAttribute] string FileName
)
void Save (
	/** @attribute OptionalAttribute() */ String FileName
)
function Save (
	FileName : String
)

Parameters

FileName

Optional. The name in which to save the project or project item.

If FileName cannot be written, such as when the disk is full or when there are write permission problems, then Save produces an error. The file is saved with a new name based on FileName.

This example works only in Visual Studio .NET 2003. For more information, see How To: Programmatically Create Projects and What's New in Extensibility and Automation in Visual Studio 2005.

public void SaveExample(DTE dte)
{
    // NOTE: This example requires a reference to the 
    //       VSLangProj namespace.

    // Create a new solution.
    Solution soln = dte.Solution;
    string solnName = "NewSolution.sln";
    string tempPath = System.IO.Path.GetTempPath();
    soln.Create(tempPath, solnName);

    // Create a new C# Console Application project.
    string templatePath = 
        dte.Solution.get_TemplatePath(PrjKind.prjKindCSharpProject);
    templatePath += "CSharpConsole.vsz";
    string projName = "NewProject";
    soln.AddFromTemplate(templatePath, tempPath + projName, 
        projName, false);
    Project proj = soln.Item(1);

    // Add a new class to the project.
    templatePath = dte.Solution.ProjectItemsTemplatePath(
        PrjKind.prjKindCSharpProject);
    templatePath += @"\CSharpAddClassWiz.vsz";
    string projItemName = "NewClass.cs";
    ProjectItem projItem = proj.ProjectItems.AddFromTemplate(
        templatePath, projItemName);

    // Add a using statement to the class file.
    TextSelection sel = (TextSelection)projItem.Document.Selection;
    sel.StartOfDocument(false);
    sel.Insert("using System.Collections;" + Environment.NewLine, 
        (int)vsInsertFlags.vsInsertFlagsCollapseToStart);

    // Save the project item, project, and solution.
    projItem.Save("");
    proj.Save(proj.FullName);
    soln.SaveAs(tempPath + solnName);
}
Show: