簡體   English   中英

枚舉當前visual studio項目中的所有文件

[英]Enumerate all files in current visual studio project

我正在嘗試編寫一個簡單的Visual Studio 2012擴展。 我已經生成了擴展模板,可以從工具菜單中彈出一個對話框。

我想枚舉當前打開的項目中的所有文件,然后根據一些規則過濾它們。 我正在尋找的是一個返回IEnumerable的代碼片段。 FileHandle應該具有以下接口或類似的東西。

interface IFileHandle {
        // Return the string 
        string Path;
        // Open the file in the editor
        void OpenEditorFor();
}

僅供參考我正在嘗試為visual studio構建模糊文件查找器。 當前文件搜索不太合適,因為您必須具有完全匹配。 我可以處理編寫索引器和模糊搜索器,但Visual Studio擴展編寫的界面目前有點神秘。

這似乎是一個簡單的答案。 在visual studio擴展的上下文中將返回所有文件。

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;
        }
    }
}

然后你就可以用它做一些巧妙的技巧,比如在我的CommandT克隆核心實現搜索功能。

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;
}

用的像

var list = RSearch("bnd", SolutionFiles().Select(x=>x.Name))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM