我不缺乏对字符串性能问题的搜索,但我仍然无法摆脱最好的方法。长期以来,我一直致力于从4NT转向PowerShell。在离开4NT时,我将错过名为FFIND的控制台超级快速字符串搜索实用程序。我决定使用我的基本C#编程技巧来尝试创建我自己的实用程序,以便在PowerShell中使用,同样快速。搜索字符串只需要第一次很长时间?
到目前为止,在几百个文件(其中一些非常大的文件)的100个目录中搜索字符串的搜索结果是FFIND 2.4秒和我的实用程序4.4秒.....在我至少运行过我的一旦????
我第一次运行它们FFIND几乎在同一时间,但我需要一分钟?这是什么?加载库?文件索引?我在代码中做错了什么?我不介意再等一会儿,但差别非常大,如果有更好的语言或方法,我宁愿在我投入太多之前开始沿着这条路走下去。
我需要选取其他语言编写一个字符串搜索将被点亮快速
我有必要利用这种工具通过文件1000来搜索网页代码,C#代码串,另一使用文本文件的预言性语言。我还需要能够使用此实用程序来查找非常大的日志文件(MB大小)中的字符串。
class Program
{
public static int linecounter;
public static int filecounter;
static void Main(string[] args)
{
//
//INIT
//
filecounter = 0;
linecounter = 0;
string word;
// Read properties from application settings.
string filelocation = Properties.Settings.Default.FavOne;
// Set Args from console.
word = args[0];
//
//Recursive search for sub folders and files
//
string startDIR;
string filename;
startDIR = Environment.CurrentDirectory;
//startDIR = "c:\\SearchStringTestDIR\\";
filename = args[1];
DirSearch(startDIR, word, filename);
Console.WriteLine(filecounter + " " + "Files found");
Console.WriteLine(linecounter + " " + "Lines found");
Console.ReadKey();
}
static void DirSearch(string dir, string word, string filename)
{
string fileline;
string ColorOne = Properties.Settings.Default.ColorOne;
string ColorTwo = Properties.Settings.Default.ColorTwo;
ConsoleColor valuecolorone = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), ColorOne);
ConsoleColor valuecolortwo = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), ColorTwo);
try
{
foreach (string f in Directory.GetFiles(dir, filename))
{
StreamReader file = new StreamReader(f);
bool t = true;
int counter = 1;
while ((fileline = file.ReadLine()) != null)
{
if (fileline.Contains(word))
{
if (t)
{
t = false;
filecounter++;
Console.ForegroundColor = valuecolorone;
Console.WriteLine(" ");
Console.WriteLine(f);
Console.ForegroundColor = valuecolortwo;
}
linecounter++;
Console.WriteLine(counter.ToString() + ". " + fileline);
}
counter++;
}
file.Close();
file = null;
}
foreach (string d in Directory.GetDirectories(dir))
{
//Console.WriteLine(d);
DirSearch(d,word,filename);
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
只是一个建议,但如果你绝望,有一个叫做grep的工具,在快速字符串搜索中是不可或缺的。 – cowboydan
本地目录或网络资源?主硬盘或可能具有省电选项的辅助硬盘(例如N分钟后关闭等)?你运行FFIND然后尝试你的(或vise verse?)另外,如果FFIND使用非托管代码,它几乎总是比加载.NET更快。 –
有很多现有的“在一堆文件中查找字符串的所有实例”程序,编写自己的程序并不值得,尤其是如果您在寻找性能。 (作为一种学习体验,当然,但不要打扰。) – JaredReisinger