枚举当前Visual Studio项目中的所有文件 [英] Enumerate all files in current visual studio project
问题描述
我正在尝试编写一个简单的Visual Studio 2012扩展.我已经生成了扩展模板,并且可以从工具菜单中弹出一个对话框.
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.
我想枚举当前打开的项目中的所有文件,然后根据一些规则对其进行过滤.我正在寻找的是返回IEnumerable的代码段.FileHandle应该具有以下接口或类似内容.
I'd like to enumerate all files in the currently open project and then filter them according to some rules. What I'm looking for is a code snippet to return IEnumerable. FileHandle should have the following interface or something similar.
interface IFileHandle {
// Return the string
string Path;
// Open the file in the editor
void OpenEditorFor();
}
仅供参考,我正在尝试为Visual Studio构建模糊文件查找器.当前文件搜索不太适合,因为您必须具有完全匹配的文件.我可以编写索引器和模糊搜索器,但是目前Visual Studio扩展编写的接口有点神秘.
FYI I'm trying to build a fuzzy file finder for visual studio. The current file search is less than suitable as you have to have exact match. I can handle writing the indexer and the fuzzy searcher but the interface to Visual Studio extension writing is a bit cryptic at the moment.
推荐答案
这似乎是简单的答案.在视觉环境中studio扩展程序将返回所有文件.
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;
}
}
}
然后,您可以用它做一些巧妙的技巧,例如在CommandT克隆的核心中实现搜索功能.
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;
}
用法类似
var list = RSearch("bnd", SolutionFiles().Select(x=>x.Name))
这篇关于枚举当前Visual Studio项目中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!