How to find a ProjectItem by the file name
May be a little late but use DTE2.Solution.FindProjectItem(fullPathofChangedFile);
I am using this user-friendly world of DTE as well, to create a Guidance. I did not find any better solution. Basically these are methods I am using:
Iterate projects:
public static ProjectItem FindSolutionItemByName(DTE dte, string name, bool recursive)
{
ProjectItem projectItem = null;
foreach (Project project in dte.Solution.Projects)
{
projectItem = FindProjectItemInProject(project, name, recursive);
if (projectItem != null)
{
break;
}
}
return projectItem;
}
Find in a single project:
public static ProjectItem FindProjectItemInProject(Project project, string name, bool recursive)
{
ProjectItem projectItem = null;
if (project.Kind != Constants.vsProjectKindSolutionItems)
{
if (project.ProjectItems != null && project.ProjectItems.Count > 0)
{
projectItem = DteHelper.FindItemByName(project.ProjectItems, name, recursive);
}
}
else
{
// if solution folder, one of its ProjectItems might be a real project
foreach (ProjectItem item in project.ProjectItems)
{
Project realProject = item.Object as Project;
if (realProject != null)
{
projectItem = FindProjectItemInProject(realProject, name, recursive);
if (projectItem != null)
{
break;
}
}
}
}
return projectItem;
}
The code I am using with more snippets could be found here, as a Guidance for new projects. Search for and take the source code..