Created
November 22, 2019 21:01
-
-
Save Sheepings/070fce180c57de06432accae04c4d9d0 to your computer and use it in GitHub Desktop.
Quick script to read file contents to ArrayList and search the ArrayList for matching contents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private static readonly string FilePath = Path.Combine(Application.StartupPath, "txt1.txt"); | |
private static readonly string ErrorLog = FilePath.Replace(Path.GetFileName(FilePath), "errorlog.log"); | |
private static readonly ArrayList ArrList = new ArrayList(); | |
private static ArrayList ReadDataToArrayList(string address) | |
{ | |
try | |
{ | |
string data = File.ReadAllText(address); | |
if (data.Length > 0 && !ArrList.Contains(data)) | |
ArrList.Add(data); | |
} | |
catch (Exception ex) | |
{ | |
/* Handle error or write error log */ | |
File.AppendAllText(ErrorLog, string.Concat(Environment.NewLine, $"Exception Message : ", | |
$"{string.Concat(Environment.NewLine, ex.Message, Environment.NewLine, "Exception Stack : ", Environment.NewLine, ex.StackTrace)} ")); | |
return ArrList; | |
} | |
return ArrList; | |
} | |
private void Button1_Click(object sender, EventArgs e) | |
{ | |
bool condition = File.Exists(FilePath); | |
if (condition) { label1.Text = $"File Exists : {condition}"; ReadDataToArrayList(FilePath); } | |
else { label1.Text = $"File Exist : {condition}"; } | |
} | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
if (ArrList.Count > 0) | |
label2.Text = ArrList[0].ToString(); | |
else label2.Text = "There was nothing to read."; | |
ArrList.Clear(); | |
} | |
private void button3_Click(object sender, EventArgs e) | |
{ | |
for (int i = 0; i < ArrList.Count; i++) | |
{ | |
if (ArrList[i].ToString().Contains(textBox1.Text)) | |
label2.Text = ArrList[i].ToString(); | |
else label2.Text = "No matches found."; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment