When searching through some files using LinqPad it can be useful to print out the results as a hyperlink that will open the file in Notepad++ at a particular line number.
public static class NotepadppExtension
{
// Usage
//string filePath = "X:/some/path/file.txt";
//filePath.CreateNotePadppHyperLink(lineNumber);
private const string NotePadppPath = @"C:\Program Files\Notepad++\notepad++.exe";
private static bool onceOnly = false;
public static Hyperlinq CreateNotepadppHyperLink(this string filePath, int lineNumber)
{
if (!onceOnly)
{
onceOnly = true;
Debug.Assert(File.Exists(NotePadppPath), $"Notepad++.exe Path: \"{NotePadppPath}\" is wrong");
}
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = NotePadppPath,
WorkingDirectory = Path.GetDirectoryName(NotePadppPath),
//Arguments = " " + filePath + " -n" + lineNumber.ToString() + " ",
};
psi.ArgumentList.Add(filePath);
psi.ArgumentList.Add("-n" + lineNumber.ToString());
var filelink = new Hyperlinq(() => Process.Start(psi), filePath);
return filelink;
}
}
Usage
string filePath = .... ;
filePath.CreateNotePadppHyperLink(lineNumber);
A search files example in Linq
void Main()
{
Directory.EnumerateFiles(
@"X:\Backup\Documents\Journals\",
"*.log", SearchOption.AllDirectories)
.SelectMany(file => TryFileReadLines(file).Select((text,n)=>
new {Text=text,LineNumber=n+1, Link=file.CreateNotepadppHyperLink(n+1)}))
.Where(line =>
//Regex.IsMatch(line.text, @"CallSearcherBase") &&
line.Text.Contains("\"Search for this text\"", StringComparison.OrdinalIgnoreCase) )
.Dump("Matches found");
}
No comments:
Post a Comment