我正试图用向导在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秩,Int32*p指标)
在System.Array.GetValue(Int32索引)
在my_wizard.IMyWizard.RunStarted(对象自动化对象,字典`2替换Dictionary,WizardRunKind runKind,Object[]customParams)
我哪里错了?如何向vs模板添加筛选器?
您可以在这里找到答案
有些人说,当解决方案资源管理器未打开时,就会出现问题
这是我基于上层链接的解决方案:
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;
}