Here is some code I'm using to search for a file that ends with a given string
.
The structure is like the Solution Explorer in Visual Studio:
Solution -> ProjectItems -> (Nested ProjectItems) -> FileNames
Wherever your code is executing, you can pull up the Projects in the Solution, and then the ProjectItems in those Projects.
var typeFileName = @"\MyClassName.cs";
// Search each Project in the Solution, exclude Unit Test Projects
foreach (Project item in _applicationObject.Solution.Projects.OfType<Project>().Where(p => !p.Name.EndsWith(".Tests")))
{
// Search each ProjectItem recursively
foreach (ProjectItem projectItem in item.ProjectItems)
{
RecursiveProjectItemSearch(projectItem, typeFileName);
}
}
asdasd
private void RecursiveProjectItemSearch(ProjectItem projectItem, string typeFileName)
{
for (short i = 0; i < projectItem.FileCount; i++)
{
var fileName = projectItem.FileNames[i];
if (fileName.EndsWith(typeFileName))
{
// In my case, I want to open the file that I'm searching for
_applicationObject.ItemOperations.OpenFile(fileName);
}
foreach(ProjectItem childProjectItem in projectItem.ProjectItems)
{
RecursiveProjectItemSearch(childProjectItem, typeFileName);
}
}
}
I don't know if this is the most optimal way to do this, but it should work. Given the code above you can change it to do a File.Open()
and read the contents or something similar.