共用方式為


Solution2.AddFromTemplate 方法 (String, String, String, Boolean)

將現有的專案檔及其所包含的一切項目或子目錄,複製至指定的位置並加入至方案中。

命名空間:  EnvDTE80
組件:  EnvDTE80 (在 EnvDTE80.dll 中)

語法

'宣告
Function AddFromTemplate ( _
    FileName As String, _
    Destination As String, _
    ProjectName As String, _
    Exclusive As Boolean _
) As Project
Project AddFromTemplate(
    string FileName,
    string Destination,
    string ProjectName,
    bool Exclusive
)
Project^ AddFromTemplate(
    [InAttribute] String^ FileName, 
    [InAttribute] String^ Destination, 
    [InAttribute] String^ ProjectName, 
    [InAttribute] bool Exclusive
)
abstract AddFromTemplate : 
        FileName:string * 
        Destination:string * 
        ProjectName:string * 
        Exclusive:bool -> Project 
function AddFromTemplate(
    FileName : String, 
    Destination : String, 
    ProjectName : String, 
    Exclusive : boolean
) : Project

參數

  • FileName
    型別:System.String
    必要項。範本專案檔的完整路徑和檔名,包含副檔名。
  • Destination
    型別:System.String
    必要項。要在其中複製 FileName 內容的目錄完整路徑。
  • ProjectName
    型別:System.String
    必要項。目的目錄中的專案檔名稱。必須包含副檔名。顯示的檔名是衍生自 ProjectName。
  • Exclusive
    型別:System.Boolean
    選擇項。指出專案要載入目前的方案還是它自己的方案。如果目前方案已關閉,且要將專案加入至新方案,則為 true;如果要將專案加入至現有的已開啟方案,則為 false。

傳回值

型別:EnvDTE.Project
Project 物件。

備註

顯示在 [方案總管] 中的專案名稱是不含副檔名的 ProjectName。如果目的端中已存在新專案的檔名,AddFromTemplate 將會失敗。

注意事項注意事項

對於 Visual Basic 和 Visual C# 專案:傳回的 Project 物件其值為 Nothing 或 nullNull 參照 (即 Visual Basic 中的 Nothing)。只要逐一查看 DTE.Solution.Projects 集合,使用 ProjectName 參數辨識新建立的專案,便可找到已建立的 Project 物件。

範例

如需如何執行增益集程式碼的詳細資訊,請參閱 HOW TO:編譯和執行 Automation 物件模型程式碼範例

下列範例會建立一個方案,並加入主控台應用程式專案。

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)
    SolutionExample(_applicationObject)
End Sub

Sub SolutionExample(ByVal dte As DTE2)
    ' This function creates a solution and adds a Visual Basic Console
    ' project to it.
    Try
        Dim soln As Solution2 = CType(DTE.Solution, Solution2)
        Dim vbTemplatePath As String
        ' This path must exist on your computer.
        ' Replace <file path> below with an actual path.
        Dim vbPrjPath As String = "<file path>"
        MsgBox("starting")
        ' Get the project template path for a Visual Basic console project.
        vbTemplatePath = soln.GetProjectTemplate _
        ("ConsoleApplication.zip", "VisualBasic")
        ' Create a new C# Console project using 
        ' the template obtained above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath, _
        "New Visual Basic Console Project", False)
        MsgBox("done")
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst,
 ref System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    SolutionExample((DTE2)_applicationObject);
}

public void SolutionExample(DTE2 dte)
{
    // This function creates a solution and adds a Visual C# Console
    // project to it.
    try{
        Solution2 soln = (Solution2)_applicationObject.Solution;
        String csTemplatePath;
        // The file path must exist on your computer.
        // Replace <file path> below with an actual path.
        String csPrjPath = "<file path>";
    "<file path>MessageBox.Show("Starting...");
"<file path>"<file path>csTemplatePath = 
soln.GetProjectTemplate("ConsoleApplication.zip", "CSharp");
        // Create a new C# Console project using the template obtained 
        // above.
        soln.AddFromTemplate(csTemplatePath, csPrjPath,
 "New CSharp Console Project", false);
        MessageBox.Show("Done!");
    }
    catch(SystemException ex)
    {
        MessageBox.Show("ERROR: " + ex);
    }
}

.NET Framework 安全性

請參閱

參考

Solution2 介面

AddFromTemplate 多載

EnvDTE80 命名空間