Share via


SolutionFolder.AddFromFile Method

Adds an existing project to the solution folder.

Namespace:  EnvDTE80
Assembly:  EnvDTE80 (in EnvDTE80.dll)

Syntax

'Declaration
Function AddFromFile ( _
    FileName As String _
) As Project
Project AddFromFile(
    string FileName
)
Project^ AddFromFile(
    [InAttribute] String^ FileName
)
abstract AddFromFile : 
        FileName:string -> Project 
function AddFromFile(
    FileName : String
) : Project

Parameters

Return Value

Type: EnvDTE.Project
A Project object.

Examples

This example creates a new solution folder and adds a project to it from an existing file. Before running this example, create a "Projects" folder off your main drive ("C:" in this example), and create a Visual C# class library project, named "ClassLibrary1" in that folder. You need to also open a project in the Visual Studio integrated development environment (IDE) before running this add-in.

For more information about how to run this example as an add-in, see How to: Compile and Run the Automation Object Model Code Examples.

Imports EnvDTE
Imports EnvDTE80
Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
 ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    solnFolderAddFromFileExample(_applicationObject)
End Sub
Sub solnFolderAddFromFileExample(ByVal dte As DTE2)
    ' Before running this example, create a "Projects" folder
    ' off your main drive (C: in this example), and create a C# 
    ' class library project, named ClassLibrary1 in that folder.
    Dim soln As Solution2 = CType(_applicationObject.Solution _
    , Solution2)
    Dim prj As Project
    Dim sb As New System.Text.StringBuilder
    Dim SF As SolutionFolder
    Try
        Dim prjPath As String = _
         "C:\Projects\ClassLibrary1\ClassLibrary1\ClassLibrary1.csproj"
        ' Open a project in the Visual Studio IDE before 
        ' running this add-in.
        ' Add a solution folder.
        prj = soln.AddSolutionFolder("A new soln folder")
        SF = CType(prj.Object, SolutionFolder)
        ' Add a project to the new solution folder.
        SF.AddFromFile(prjPath)
        MsgBox("Added a new solution folder that contains  _
        a C# project named ClassLibrary1.")
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    solnFolderAddFromFileExample(_applicationObject);
}
public void solnFolderAddFromFileExample(DTE2 dte)
{
    // Before running this example, create a "Projects" folder
    // off your main drive (C: in this example), and create a C# 
    // class library project, named ClassLibrary1 in that folder.
    Solution2 soln = (Solution2)_applicationObject.Solution;
    Project prj;
    SolutionFolder SF;
    try
    {
        String prjPath =
 "C:\\Projects\\ClassLibrary1\\ClassLibrary1\\ClassLibrary1.csproj";
        // Open a project in the Visual Studio IDE before 
        // running this add-in.
        // Add a solution folder.
        prj = soln.AddSolutionFolder("A new soln folder");
        SF = (SolutionFolder)prj.Object;
        // Add a project to the new solution folder.
        SF.AddFromFile(prjPath);
        MessageBox.Show("Added a new solution folder that contains a 
C# project named ClassLibrary1.");
    }
    catch(SystemException ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

.NET Framework Security

See Also

Reference

SolutionFolder Interface

EnvDTE80 Namespace