Get file names in VS2010 project using T4
I'm trying to use T4 with VS2010 to enumerate files in a project, run a custom tool on files that have a certain extension, and then add generated files to the solution. However, I can't seem to figure out how to get the names of开发者_开发技巧 the files in my projects. I looked at EnvDTE and couldn't locate a function that would do this. Does my approach even make sense? I'm trying to use T4 more like a VS2010 scripting tool than anything else.
Good luck. No, seriously. EnvDTE isn't the most friendly interface.
Long story short, from the Solution EnvDTE.Solution you must iterate the projects within the solution (Solution.Projects) and then enumerate (recursively!) the ProjectItems in the Project (Project.ProjectItems) and get the filenames for each ProjectItem (ProjectItem.FileNames).
There are many stumbling points involved in this. ProjectItems may represent the References node in the Project, a folder, or other items which don't actually have files associated with them. Also, a single ProjectItem may have multiple files (think of class.cs and class.designer.cs). And, to top it all off, DTE is just a crappy COM wrapper and feels like one.
精彩评论