我有一个表单想要至少有一个关键字,比如 download 。
这是调用我的OpenFileDialog的代码。
该班级为Form1
public Form1()
{
InitializeComponent();
initListView();
}
private void button1_Click(object sender, EventArgs e)
{
getallfiles.getSelectedFiles(this.listView1);
}
private void initListView()
{
listView1.View = View.Details;
listView1.Columns.Add("Filename", 210, HorizontalAlignment.Left);
listView1.Columns.Add("Size", 60, HorizontalAlignment.Left);
listView1.Columns.Add("Match", 60, HorizontalAlignment.Left);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
listView1.Items.Clear();
seaching.matchResult(progressBar1, listView1, textBox1.Text, textBox2.Text);
}
catch (Exception f)
{
MessageBox.Show(f.ToString());
}
}
现在我获取多选文件并将它们存储在filePaths
String []中。
该班级为getallfiles
public static String[] filePaths;
public static void getSelectedFiles(ListView listView)
{
OpenFileDialog openAllFiles = new OpenFileDialog();
openAllFiles.Title = "Select all files you want to look for keywords";
openAllFiles.Multiselect = true;
openAllFiles.Filter = "(*.*)|*.*";
if (openAllFiles.ShowDialog() == DialogResult.OK)
{
filePaths = new string[openAllFiles.FileNames.Length];
int count = 0;
foreach (String file in openAllFiles.FileNames)
{
try
{
ListViewItem masterItem = new ListViewItem(new[] { Path.GetFileName(file), (Math.Round(((new FileInfo(file).Length) / 1024f), 3).ToString()) + " kb"});
listView.Items.Add(masterItem);
filePaths[count] = file;
count++;
}
catch (Exception f)
{
MessageBox.Show(f.ToString());
}
}
}
}
现在我的问题代码。如果只写了关键字,我可以过滤,但如果下载,我将关键字设置为下载,我的解决方案就会停止工作。
该班级为searching
public static void matchResult(ProgressBar progressBar, ListView listView, String key1, String key2)
{
progressBar.Maximum = getallfiles.filePaths.Length;
progressBar.Value = 0;
int count = 0;
bool lockCount = false;
String fileText;
foreach (string file in getallfiles.filePaths)
{
using (StreamReader reader = new StreamReader(getallfiles.filePaths[count]))
{
while ((fileText = reader.ReadLine()) != null)
{
if (String.IsNullOrEmpty(key2) & !String.IsNullOrEmpty(key1))
{
if (fileText.Contains(key1.ToLower()) | fileText.Contains(key1.ToUpper()) | fileText.Contains(key1))
{
ListViewItem masterItem = new ListViewItem(new[] { Path.GetFileName(file), (Math.Round(((new FileInfo(file).Length) / 1024f), 3).ToString()) + " kb" });
listView.Items.Add(masterItem);
count++;
lockCount = true;
reader.Close();
break;
}
}
else if(!String.IsNullOrEmpty(key2))
{
if (fileText.Contains(key1.ToLower()) | fileText.Contains(key2.ToLower()) | fileText.Contains(key1.ToUpper()) | fileText.Contains(key2.ToUpper()) | fileText.Contains(key1) | fileText.Contains(key2))
{
ListViewItem masterItem = new ListViewItem(new[] { Path.GetFileName(file), (Math.Round(((new FileInfo(file).Length) / 1024f), 3).ToString()) + " kb" });
listView.Items.Add(masterItem);
count++;
lockCount = true;
reader.Close();
break;
}
}
}
if (!lockCount)
{
count++;
}
}
}
}
我的下一个想法是检查Char [],但我不确定这是否有效。 我让 char 读取 char 并将当前行存储在char []中并比较我正在寻找的char []是否在那里。