/// <summary> /// Returns true when the <paramref name="textToFind"/> is found in the /// <paramref name="fileName"/> /// </summary> /// <param name="fileName">The file to inspect</param> /// <param name="textToFind">The text to find</param> /// <param name="ignoreCase">Set to false to search case sensitive</param> /// <returns></returns> public bool FileContainsText(string fileName, string textToFind, bool ignoreCase = true) { using (var reader = new FilterReader(fileName)) { string line; while ((line = reader.ReadLine()) != null) { if (string.IsNullOrEmpty(line)) continue; if (ignoreCase) { if (line.IndexOf(textToFind, StringComparison.InvariantCultureIgnoreCase) >= 0) return true; } else { if (line.IndexOf(textToFind, StringComparison.InvariantCulture) >= 0) return true; } } } return false; }
/// <summary> /// Returns true when the <paramref name="regularExpression"/> is found in the /// <paramref name="fileName"/> /// </summary> /// <param name="fileName">The file to inspect</param> /// <param name="regularExpression">The regular expression to use</param> /// <param name="ignoreCase">Set to false to search case sensitive</param> /// <returns></returns> public bool FileContainsRegexMatch(string fileName, string regularExpression, bool ignoreCase = true) { var regex = new Regex(regularExpression, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); using (var reader = new FilterReader(fileName)) { var line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) return false; if (regex.IsMatch(line)) return true; } return false; }
/// <summary> /// Returns true when at least one of the given <paramref name="textToFind"/> is found /// in the <paramref name="fileName"/> /// </summary> /// <param name="fileName">The file to inspect</param> /// <param name="textToFind">The array with one or more text to find</param> /// <param name="ignoreCase">Set to false to search case sensitive</param> /// <returns></returns> public bool FileContainsText(string fileName, string[] textToFind, bool ignoreCase = true) { using (var reader = new FilterReader(fileName)) { var line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) return false; if (ignoreCase) line = line.ToUpperInvariant(); if (textToFind.Any(text => line.Contains(text))) return true; } return false; }
private void SelectButton_Click(object sender, EventArgs e) { // Create an instance of the open file dialog box. var openFileDialog1 = new OpenFileDialog { // ReSharper disable once LocalizableElement Filter = "Alle files (*.*)|*.*", FilterIndex = 1, Multiselect = false }; // Process input if the user clicked OK. if (openFileDialog1.ShowDialog() == DialogResult.OK) { FileLabel.Text = openFileDialog1.FileName; FindTextButton.Enabled = true; TextToFindTextBox.Enabled = true; FindWithRegexButton.Enabled = true; TextToFindWithRegexTextBox.Enabled = true; try { DisableInput(); FilterTextBox.AppendText("*** Processing file '" + openFileDialog1.FileName + "' ***" + Environment.NewLine + Environment.NewLine); Application.DoEvents(); var stopWatch = new Stopwatch(); using ( var reader = new FilterReader(openFileDialog1.FileName, string.Empty, DisableEmbeddedContentCheckBox.Checked, IncludePropertiesCheckBox.Checked, ReadIntoMemoryCheckBox.Checked)) { stopWatch.Start(); string line; while ((line = reader.ReadLine()) != null) { FilterTextBox.AppendText(line + Environment.NewLine); Application.DoEvents(); } stopWatch.Stop(); FilterTextBox.AppendText(Environment.NewLine + "*** DONE IN " + stopWatch.Elapsed + " ***" + Environment.NewLine); Application.DoEvents(); } } catch (Exception exception) { DisableInput(); FilterTextBox.Text = exception.StackTrace + Environment.NewLine + GetInnerException(exception); } finally { EnableInput(); } } }
/// <summary> /// Returns an array with all the matches that are found with the give <see cref="regularExpression"/> regular expression /// </summary> /// <param name="fileName">The file to inspect</param> /// <param name="regularExpression">The regular expression to use</param> /// <param name="ignoreCase">Set to false to search case sensitive</param> /// <returns></returns> public string[] GetRegexMatchesFromFile(string fileName, string regularExpression, bool ignoreCase = true) { var regex = new Regex(regularExpression, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); var result = new List<string>(); using (var reader = new FilterReader(fileName)) { var text = reader.ReadToEnd(); result.AddRange(from Match match in regex.Matches(text) select match.ToString()); } return result.ToArray(); }
/// <summary> /// Returns all the text that is inside the <paramref name="fileName"/> /// </summary> /// <param name="fileName">The file to read</param> /// <returns></returns> public string GetAllText(string fileName) { using (var reader = new FilterReader(fileName)) return reader.ReadToEnd(); }
/// <summary> /// Returns true when at least one of the given <paramref name="textToFind"/> is found /// in the <paramref name="fileName"/> /// </summary> /// <param name="fileName">The file to inspect</param> /// <param name="textToFind">The array with one or more text to find</param> /// <param name="ignoreCase">Set to false to search case sensitive</param> /// <returns></returns> public bool FileContainsTextFromArray(string fileName, string[] textToFind, bool ignoreCase = true) { using (var reader = new FilterReader(fileName)) { string line; while ((line = reader.ReadLine()) != null) { if (string.IsNullOrEmpty(line)) continue; if (ignoreCase) line = line.ToUpperInvariant(); foreach (var text in textToFind) { var temp = text; if (ignoreCase) temp = text.ToUpperInvariant(); if (line.Contains(temp)) return true; } } } return false; }