Enumerate all files in current visual studio project

前端 未结 1 1482
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 20:48

I\'m trying to write a simple Visual Studio 2012 extension. I have generated the extension template and can bring up a dialog box from a tool menu.

I\'d like to enum

相关标签:
1条回答
  • 2021-01-14 20:49

    This seems to be the simple answer. In the context of a visual studio extension will return all files.

    public IEnumerable<ProjectItem> Recurse(ProjectItems i)
    {
        if (i!=null)
        {
            foreach (ProjectItem j in i)
            {
                foreach (ProjectItem k in Recurse(j))
                {
                    yield return k;
                }
            }
    
        }
    }
    public IEnumerable<ProjectItem> Recurse(ProjectItem i)
    {
        yield return i;
        foreach (ProjectItem j in Recurse(i.ProjectItems ))
        {
            yield return j;
        }
    }
    
    public IEnumerable<ProjectItem> SolutionFiles()
    {
        Solution2 soln = (Solution2)_applicationObject.Solution;
        foreach (Project project in soln.Projects)
        {
            foreach (ProjectItem item in Recurse(project.ProjectItems))
            {
                yield return item;
            }
        }
    }
    

    You can then do neat tricks with it like implement the search function at the core of my CommandT clone.

    private static string Pattern(string src)
    {
        return ".*" + String.Join(".*", src.ToCharArray());
    }
    
    private static bool RMatch(string src, string dest)
    {
        try
        {
            return Regex.Match(dest, Pattern(src), RegexOptions.IgnoreCase).Success;
        }
        catch (Exception e)
        {
            return false;
        }
    }
    
    private static List<string> RSearch(
    string word,
    IEnumerable<string> wordList,
    double fuzzyness)
    {
        // Tests have prove that the !LINQ-variant is about 3 times
        // faster!
        List<string> foundWords =
            (
                from s in wordList
                where RMatch(word, s) == true
                orderby s.Length ascending 
                select s
            ).ToList();
    
        return foundWords;
    }
    

    which is used like

    var list = RSearch("bnd", SolutionFiles().Select(x=>x.Name))
    
    0 讨论(0)
提交回复
热议问题