0

I am new to c# and I am trying to write simple code for listing all .ini files in directories. And now I need each line to be green or red depending on the context of .ini files. My code seems like just go through all files in listbox and color all lines depending on value of the last .ini. Thanks

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string rootdir = @"C:\Users\isaced1\Desktop\test";

        string[] files = Directory.GetFiles(rootdir, "*.ini", SearchOption.AllDirectories);
        Projects.Items.AddRange(files);
        //var items = Projects.SelectedItems;
        foreach (var item in files)
        {
            try
            {
                string fileName = Projects.GetItemText(item);
                string fileContents = System.IO.File.ReadAllText(fileName);
                const string PATTERN = @"OTPM              = true";
                Match match = Regex.Match(fileContents, PATTERN, RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    Projects.ForeColor = Color.Green;
                }
                else
                {
                    Projects.ForeColor = Color.Red;
                }
            }
            catch
            {
                MessageBox.Show("No");
            }
        }

    }
}
Katonas
  • 39
  • 3
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Sep 08 '22 at 10:54
  • `ListBox` does not support coloring each line with a different color out of the box. You can either draw the items yourself using `OwnerDraw`, or switch to `ListView`. I believe the latter will be much simpler. – wohlstad Sep 08 '22 at 10:58
  • Does this answer your question? [C# : changing listbox row color?](https://stackoverflow.com/questions/2554609/c-sharp-changing-listbox-row-color) – wohlstad Sep 08 '22 at 10:59

0 Answers0