Добавить фильтр в шаблон визуальной студии

Я пытаюсь создать фильтр (одна из маленьких папок, которая не выполняет ничего, кроме отдельных файлов в проекте) в шаблоне проекта 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(Void* elemRef, Int32 rank, Int32* pIndices)

в System.Array.GetValue(индекс Int32)

в my_wizard.IMyWizard.RunStarted(Объект автоматизации объекта, словарь`2 replacementsDictionary, WizardRunKind runKind, Object[] customParams)

Где я ошибаюсь? Как я могу добавить фильтр против шаблона?

1 ответ

Решение

Вы можете найти ответ здесь.
Некоторые говорят, что проблема возникает, когда Solution Explorer не открыт.
Вот мое решение, основанное на верхней ссылке:

    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;
    }
Другие вопросы по тегам