【发布时间】:2016-09-30 01:12:17
【问题描述】:
使用用户输入文本框,我想搜索目录中的哪个文件包含该文本。然后我想解析出信息
但我似乎无法找到字符串或至少无法返回信息。任何帮助将不胜感激。
我当前的代码:
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);
}
}
}
}
}
}
}
}
【问题讨论】:
-
问题是什么?
-
如何让用户从 txt 框中输入 txt 以搜索目录中的所有 .txt 文件并从文件中提取该行以便我可以解析数据。
-
一目了然,这就是代码已经做了。
-
我是这么想的,但是当我运行应用程序时,lblOutput.text 仍然为空,
-
所以使用调试器单步调试代码。
标签: c#