[英]C# search all files in a directory that contain a string, then return that string
Using user input into a textbox, I want to search for which file in the directory contains that text.使用文本框中的用户输入,我想搜索目录中的哪个文件包含该文本。 I would then like to parse out the information然后我想解析出信息
but I can't seem to find the string or at least return the information.但我似乎找不到字符串或至少返回信息。 Any help would be greatly appreciated.任何帮助将不胜感激。
My current code:我当前的代码:
private void btnSearchSerial_Click(object sender, EventArgs e)
{
dynamic dirScanner = @"\\mypath\";
string strSerial;
string strSID;
string strInputLine;
string strOutput;
strSerial = Convert.ToString(txtSerialSearch);
strSID = Convert.ToString(txtSID);
if (txtSerialSearch.Text != "" && txtSID.Text != "")
{
try
{
string[] allFiles = Directory.GetFiles(dirScanner);
foreach (string file in allFiles)
{
if (file.EndsWith(".txt"))
{
using (StreamReader sr = new StreamReader(file))
{
while (sr.Peek() >= 0)
{
strInputLine = sr.ReadLine();
if (strInputLine.Contains(strSerial))
{
strOutput = Convert.ToString(strInputLine);
lblOutput.Text = Convert.ToString(strOutput);
}
}
}
}
}
}
}
}
You seem quite lost. 你好像很迷茫。 Why are you using a dynamic
when a string
is all that you need? 当你需要一个string
时,为什么使用dynamic
? Your code has too many unnecessary variables and convertions. 您的代码有太多不必要的变量和转换。 Here's a much simpler way to do it. 这是一种更简单的方法。 I don't know what you want the label to have if there are many matching lines, here I'm only placing the first one there: 如果有很多匹配的线,我不知道你想要的标签,这里我只放置第一个:
string dirScanner = @"\\mypath\";
if (string.IsNullOrWhiteSpace(txtSerialSearch.Text) || string.IsNullOrWhiteSpace(txtSID.Text))
return;
string[] allFiles = Directory.GetFiles(dirScanner, "*.txt");
foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);
string firstOccurrence = lines.FirstOrDefault(l => l.Contains(txtSerialSearch.Text));
if (firstOccurrence != null)
{
lblOutput.Text = firstOccurrence;
break;
}
}
I have implemented the same using Regular Expressions . 我使用正则表达式实现了相同的功能。 You need to use namespace using System.Text.RegularExpressions;
您需要using System.Text.RegularExpressions;
来使用命名空间using System.Text.RegularExpressions;
string strSerial = @"Microsoft";
Regex match = new Regex(strSerial);
string matchinglines = string.Empty;
List<string> filenames = new List<string>(Directory.GetFiles(textBox1.Text));
foreach(string filename in filenames)
{
//StreamReader strFile = new StreamReader(filename);
string fileContent = File.ReadAllText(filename);
if(match.IsMatch(fileContent))
{
label1.Text = Regex.Match(fileContent, strSerial).ToString();
break;
}
}
Use System.LINQ:使用 System.LINQ:
private IEnumerable<string> GetFilesWithText(string your_text, string dir)
{
var list_of_files_that_match = Directory.EnumerateFiles(dir).Where(delegate (string t)
{
if (Path.GetExtension(t).Contains("txt"))
return System.IO.File.ReadAllText(t).Contains(your_text);
return false;
});
return list_of_files_that_match;
// if this is empty then there were not matches.
}
This worked for me.这对我有用。 Quick and simple.快速简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.