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

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

命名空間:  EnvDTE100
組件:  EnvDTE100 (在 EnvDTE100.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 物件。

實作

Solution3.AddFromTemplate(String, String, String, Boolean)

備註

顯示在 [方案總管] 中的專案名稱是不含副檔名的 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 C# Console
    ' project to it.
    Try
        Dim soln As Solution4 = CType(DTE.Solution, Solution4)
        Dim csTemplatePath As String
        ' This path must exist on your computer.
        ' Replace <file path> below with an actual path.
        Dim csPrjPath As String = "<file path>"
        MsgBox("starting")
        ' Get the project template path for a C# console project.
        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)
        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{
        Solution4 soln = (Solution4)_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 安全性

請參閱

參考

Solution4 介面

AddFromTemplate 多載

EnvDTE100 命名空間

其他資源

HOW TO:編譯和執行 Automation 物件模型程式碼範例