要为任何用户的文件结构通用工作,您需要枚举所有项目中的所有文件。这应该让你开始。而且,好吧,差不多完成了:-)
    internal static string GetSourceOrInclude(bool openAndActivate)
    {
        // Look in the project for a file of the same name with the opposing extension
        ProjectItem thisItem = Commands.Application.ActiveDocument.ProjectItem;
        string ext = Path.GetExtension(thisItem.Name);
        string searchExt = string.Empty;
        if (ext == ".cpp" || ext == ".c")
            searchExt = ".h";
        else if (ext == ".h" || ext == ".hpp")
            searchExt = ".cpp";
        else
            return(string.Empty);
        string searchItemName = thisItem.Name;
        searchItemName = Path.ChangeExtension(searchItemName, searchExt);
        Project proj = thisItem.ContainingProject;
        foreach(ProjectItem item in proj.ProjectItems)
        {
            ProjectItem foundItem = FindChildProjectItem(item, searchItemName);
            if (foundItem != null)
            {
                if (openAndActivate)
                {
                    if (!foundItem.get_IsOpen(Constants.vsViewKindCode))
                    {
                        Window w = foundItem.Open(Constants.vsViewKindCode);
                        w.Visible = true;
                        w.Activate();
                    }
                    else
                    {
                        foundItem.Document.Activate();
                    }
                }
                return(foundItem.Document.FullName);
            }
        return(string.Empty);
    }
请注意,标头可能在包含路径中而不被添加到项目中,因此如果上述失败,您也可能会查看包含项目的包含路径。我将把它作为练习留给读者。