0

我試圖創建一個過濾器(其中一個小文件夾什麼都不做,但在一個項目中分開文件)在Visual Studio(C++)項目模板與嚮導,所以我在RunStarted方法中寫入以下代碼:添加篩選器到視覺工作室模板

public void RunStarted(object automationObject, 
     Dictionary<string, string> replacementsDictionary, 
     WizardRunKind runKind, object[] customParams) 
    { 
     try 
     { 
     // Add filters to the project 
     EnvDTE.DTE dte = (EnvDTE.DTE)automationObject; 

     Array activeProjects = (Array)dte.ActiveSolutionProjects; 
     Project activeProj = (Project)activeProjects.GetValue(0); 
     VCProject prj = (VCProject)activeProj.ProjectItems.Item(0); 
     VCFilter filter = prj.AddFilter("Header_Files"); 
     filter.AddFile("header.h"); 
     prj.Save(); 
     } 
     catch (Exception ex) 
     { 
     MessageBox.Show(ex.ToString()); 
     } 
    } 

雖然這是失敗。返回的錯誤是:

System.IndexOutOfRangeException:索引超出了 數組的範圍。

在System.Array.InternalGetReference(無效* elemRef,的Int32秩, 的Int32 * pIndices)

在System.Array.GetValue(的Int32索引)

在my_wizard.IMyWizard.RunStarted(對象automationObject , Dictionary`2 replacementsDictionary,WizardRunKind runKind,對象[] customParams)

當我收到錯了嗎?我如何將過濾器添加到vs模板?

回答

1

你可以找到答案here
有人說解決方案資源管理器沒有打開時就會出現這個問題。
這裏是我的解決方案基於上面的鏈接:

private Project getActiveProject(DTE2 dte) 
    { 
     Array projects = dte.ActiveSolutionProjects as Array; 
     if (projects == null || projects.Length == 0) 
     { 
      projects = dte.Solution.SolutionBuild.StartupProjects as Array; 
      if (projects == null || projects.Length == 0) 
      { 
       Projects pro = dte.Solution.Projects; 
       if (pro == null || pro.Count == 0) 
        return null; 
       return pro.Item(0); 
      } 
     } 
     return projects.GetValue(0) as Project; 
    }