2016-05-05 2 views
4

ファイルソリューションの種類にかかわらず、VSソリューション内のすべてのファイルに対してプログラムでテキスト検索を実行するにはどうすればよいですか。「Find in All Files」などのVisual Studio API /関数にアクセスして呼び出す方法。

Visual Studioは enter image description here

が同じことを行うためにVS APIを使用することが可能であり、「すべてのファイル内の検索」を介してこれを行うことができますか?

答えて

0

は...検索の多くの後

// DTE

API

を検索DTEとVSを使用する方法
namespace Utilities 
{ 
using System; 
using System.IO; 
using System.Linq; 
using System.Collections.Generic; 

using EnvDTE; 
using EnvDTE80; 

using Microsoft.VisualStudio.Shell; 

static class DteExtensions 
{ 
    private static Dictionary<string, Project> CachedProjectsFromPath = new Dictionary<string, Project>(); 
    private static IEnumerable<Project> _projects; 
    private static DTE2 _dte; 

    static DteExtensions() 
    { 
     if (DTE == null) return; 

     DTE.Events.SolutionEvents.ProjectRemoved += delegate { _projects = null; }; 
     DTE.Events.SolutionEvents.ProjectRenamed += delegate { _projects = null; }; 
     DTE.Events.SolutionEvents.ProjectAdded += delegate { _projects = null; }; 
    } 

    internal static DTE2 DTE => _dte ?? (_dte = ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE2); 

    internal static string SolutionName 
    { 
     get 
     { 
      return Path.GetFileNameWithoutExtension(DTE.Solution.FullName); 
     } 
    } 

    internal static string SolutionFullPath 
    { 
     get 
     { 
      return Path.GetFullPath(DTE.Solution.FullName); 
     } 
    } 

    internal static string SolutionPath 
    { 
     get 
     { 
      var path = DTE?.Solution?.FullName; 
      return path.IsEmpty() ? string.Empty : Path.GetDirectoryName(path); 
     } 
    } 

    internal static IEnumerable<Project> Projects => _projects ?? (_projects = DTE.Solution.Projects.OfType<Project>().SelectMany(GetProjects)); 

    internal static string CurrentProjectName => DTE?.ActiveDocument?.ProjectItem?.ContainingProject?.Name; 

    internal static Project GetProjectFromFilePath(string filePath) 
    { 
     if (CachedProjectsFromPath.ContainsKey(filePath)) 
      return CachedProjectsFromPath[filePath]; 

     var project = Projects.ToDictionary(p => p, p => (filePath.IndexOf(Path.GetDirectoryName(p.FullName)) >= 0) ? Path.GetDirectoryName(p.FullName).Length : 0) 
      .Aggregate((i1, i2) => i1.Value > i2.Value ? i1 : i2); 

     CachedProjectsFromPath.Add(filePath, project.Key); 

     return project.Key; 
    } 

    private static IEnumerable<Project> GetProjects(Project projectItem) 
    { 
     var projects = new List<Project>(); 

     if (projectItem == null) 
      return projects; 

     try 
     { 
      // Project 
      var projectFileName = projectItem.FileName; 

      if (projectFileName.HasValue() && File.Exists(projectFileName)) 
      { 
       projects.Add(projectItem); 
      } 
      else 
      { 
       // Folder 
       for (int i = 1; i <= projectItem.ProjectItems.Count; i++) 
       { 
        foreach (var item in GetProjects(projectItem.ProjectItems.Item(i).Object as Project)) 
        { 
         projects.Add(item); 
        } 
       } 
      } 
     } 
     catch 
     { 
      //No logging is needed 
     } 

     return projects; 
    } 
} 

}

//の定義私の結果であり、

void VisualStudioFindAllFiles(string methodName) 
    { 

     // Get an instance of the currently running Visual Studio IDE. 
     var objFind = DteExtensions.DTE.Find; 

     //Set the find options    
     objFind.Action = EnvDTE.vsFindAction.vsFindActionFindAll; 
     objFind.Backwards = false; 
     //objFind.FilesOfType = $"*.{fileType}"; 
     objFind.FindWhat = methodName; 
     //objFind.KeepModifiedDocumentsOpen = true; 
     objFind.MatchCase = false; 
     objFind.MatchInHiddenText = true; 
     objFind.MatchWholeWord = true; 
     objFind.PatternSyntax = EnvDTE.vsFindPatternSyntax.vsFindPatternSyntaxLiteral; 
     objFind.ResultsLocation = EnvDTE.vsFindResultsLocation.vsFindResultsNone; 
     objFind.SearchPath = DteExtensions.SolutionFullPath; 
     objFind.SearchSubfolders = true; 
     objFind.Target = EnvDTE.vsFindTarget.vsFindTargetSolution; 

     //Perform the Find operation. 
     var res = objFind.Execute(); 

    } 
1

VSAPでは、DTE.Findオブジェクトを使用して検索パラメータを設定し、Execute()を呼び出すことができます。 C#を使用してVS APIにアクセスするには、COMを使用して独自のプロセスから呼び出したり、Visual Studioの拡張機能やVisual Commanderのコマンド(Prompt for a search string and list all matching lines from the current fileなど)を記述することができます(これはVBの例です)。ここで

関連する問題