Katonas
Member
- Joined
- Sep 6, 2022
- Messages
- 6
- Programming Experience
- Beginner
Hello
I have code which search in directories for specific .ini files and depending on its content adds it to listbox 1 or listbox2. I also have button who can change content in .ini files I selected in my listbox1 or listbox2. But once I change content in .ini file it should move between my listboxes, basicaly I sort .ini files by true or false statement and then i change that statement I want it to move. I tried listox1.update(); or listbox1.refresh() comands. it doesnt work for me.
my code
I have code which search in directories for specific .ini files and depending on its content adds it to listbox 1 or listbox2. I also have button who can change content in .ini files I selected in my listbox1 or listbox2. But once I change content in .ini file it should move between my listboxes, basicaly I sort .ini files by true or false statement and then i change that statement I want it to move. I tried listox1.update(); or listbox1.refresh() comands. it doesnt work for me.
my code
C#:
private void Form1_Load(object sender, EventArgs e)
{
string rootdir = @"C:\Users\isaced1\Desktop\test";
string[] files = Directory.GetFiles(rootdir, "*.ini", SearchOption.AllDirectories);
foreach (string item in files)
{
string fileContents = File.ReadAllText(item);
const string PATTERN = @"OTPM = true";
Match match = Regex.Match(fileContents, PATTERN, RegexOptions.IgnoreCase);
if (match.Success)
{
listBox1.Items.Add(item);
listBox1.ForeColor = Color.Green;
}
else
{
listBox2.Items.Add(item);
listBox2.ForeColor = Color.Red;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
var items = listBox2.SelectedItems;
foreach (var item in items)
{
string fileName = listBox2.GetItemText(item);
string text = File.ReadAllText(fileName);
const string PATTERN = @"OTPM = (?<Number>[false]+)";
Match match = Regex.Match(text, PATTERN, RegexOptions.IgnoreCase);
string otpmT = "true";
if (match.Success)
{
int index = match.Groups["Number"].Index;
int length = match.Groups["Number"].Length;
text = text.Remove(index, length);
text = text.Insert(index, otpmT.ToString());
File.WriteAllText(fileName, text);
Process.Start(fileName);
}
}
var items1 = listBox1.SelectedItems;
foreach (var item in items1)
{
string fileName = listBox1.GetItemText(item);
string text = File.ReadAllText(fileName);
const string PATTERN = @"OTPM = (?<Number>[true]+)";
Match match = Regex.Match(text, PATTERN, RegexOptions.IgnoreCase);
string otpmF = "false";
if (match.Success)
{
int index = match.Groups["Number"].Index;
int length = match.Groups["Number"].Length;
text = text.Remove(index, length);
text = text.Insert(index, otpmF.ToString());
File.WriteAllText(fileName, text);
Process.Start(fileName);
}
}
}