Visual Studio Development Bookmark and Share   
 index > Visual Studio Guidance Automation Toolkit > Problem getting project items from project (using DteHelper.FindProjectByName)
 

Problem getting project items from project (using DteHelper.FindProjectByName)

Hello,

I´m tring to edit the code of an existing file in a project.
My main problem is that i cannot find this file in the projectItems of the project.
I am using the following method to get a reference to the file:



DTE vs = base.GetService<DTE>(true);
Project proj = DteHelper.FindProjectByName(vs, projectName);
ProjectItem projItem = DteHelper.FindItemByName(proj.ProjectItems, fileName, true);



The BIG problem is thatproj.ProjectItem is EMPTY... And, obviously, projItem will be null...

Can anyone help me or at least tell me another way to get a project item from a project using only the project and project item names?

Thank you.

jrebelo  Wednesday, January 28, 2009 10:51 AM
Hi,

I had the similar problem some time ago.

I can recommend you two tecniques to solve the problem.

I uploaded the DTEHelper code, you can download it from this link:

DTEHelper.cs

you can add this class to your project and then you can use it to replace the original.

Probably you are using the DTEHelper from Microsoft.Practices.RecipeFramework.Library, the class code that I uploaded is the same.

You can add this class and then debug the code, and see Why isn't the function working?

Remember to change the namespace.

Other method is to write you own helper code, I can see that if you use the "foreach", it works always.

publicstaticclassDTEHelper
{
publicstaticProjectFindProjectByName(_DTEvs,stringname)
{
foreach(Projectprojectinvs.Solution.Projects)
{
if(project.Name==name)
returnproject;
}
returnnull;
}
}

I hope that help

greetings


Leandro Tuttini
  • Marked As Answer byjrebelo Thursday, January 29, 2009 7:34 PM
  •  
Leandro Tuttini  Wednesday, January 28, 2009 2:12 PM

Hi!

tks for the answer! Rigth now, before checking answers here, i was able to come up with a solution.

It happens that the several signatures for theFindProject method in the DteHelper class can return different results. The problematic approach that i exposed in the beginning of the thread returned INCOMPLETE information on the Project object.But the next code returns the complete information(including a full ProjectItem list):

public static Project FindProject(DTE dte, string name)

{

Predicate<Project> predicate = new Predicate<Project>(delegate(Project p)

{

return p.UniqueName.EndsWith('\\' + name, StringComparison.CurrentCultureIgnoreCase) ||

p.UniqueName.EndsWith('\\' + name + ".csproj", StringComparison.CurrentCultureIgnoreCase) ||

p.UniqueName.StartsWith(name + '\\', StringComparison.CurrentCultureIgnoreCase);

});

return DteHelper.FindProject(dte, predicate);

}

Grazzi tanti Leandro!

  • Marked As Answer byjrebelo Thursday, January 29, 2009 7:34 PM
  •  
jrebelo  Thursday, January 29, 2009 7:34 PM
Hi,

I had the similar problem some time ago.

I can recommend you two tecniques to solve the problem.

I uploaded the DTEHelper code, you can download it from this link:

DTEHelper.cs

you can add this class to your project and then you can use it to replace the original.

Probably you are using the DTEHelper from Microsoft.Practices.RecipeFramework.Library, the class code that I uploaded is the same.

You can add this class and then debug the code, and see Why isn't the function working?

Remember to change the namespace.

Other method is to write you own helper code, I can see that if you use the "foreach", it works always.

publicstaticclassDTEHelper
{
publicstaticProjectFindProjectByName(_DTEvs,stringname)
{
foreach(Projectprojectinvs.Solution.Projects)
{
if(project.Name==name)
returnproject;
}
returnnull;
}
}

I hope that help

greetings


Leandro Tuttini
  • Marked As Answer byjrebelo Thursday, January 29, 2009 7:34 PM
  •  
Leandro Tuttini  Wednesday, January 28, 2009 2:12 PM

Hi!

tks for the answer! Rigth now, before checking answers here, i was able to come up with a solution.

It happens that the several signatures for theFindProject method in the DteHelper class can return different results. The problematic approach that i exposed in the beginning of the thread returned INCOMPLETE information on the Project object.But the next code returns the complete information(including a full ProjectItem list):

public static Project FindProject(DTE dte, string name)

{

Predicate<Project> predicate = new Predicate<Project>(delegate(Project p)

{

return p.UniqueName.EndsWith('\\' + name, StringComparison.CurrentCultureIgnoreCase) ||

p.UniqueName.EndsWith('\\' + name + ".csproj", StringComparison.CurrentCultureIgnoreCase) ||

p.UniqueName.StartsWith(name + '\\', StringComparison.CurrentCultureIgnoreCase);

});

return DteHelper.FindProject(dte, predicate);

}

Grazzi tanti Leandro!

  • Marked As Answer byjrebelo Thursday, January 29, 2009 7:34 PM
  •  
jrebelo  Thursday, January 29, 2009 7:34 PM

You can use google to search for other answers

Custom Search

More Threads

• Enable GAT for existing projects
• Guidance Automation Toolkit July 2007 Bug
• Can't see item templates for WebApplication projects
• Guidance Package Visual Studio 2005 Project Template not installed on Vista 5456
• Access Denied error on Install of Feb GAT
• How to use devenv to build specific configuration in VS2005?
• Include file with "Add as a link"
• Creating Data Repository classes.
• Unfold Recipe vs New Item
• How to set the Open attribute of AddItemFromStringAction