[C#] Saving a Config Tutorial (Reading & Writing to Text)

Harry

Certified Sick ℂunt
Premium Member
Messages
1,253
Reaction score
942
Points
973
Hey CCM,
Haven't made a thread in a while ey??
Today I'm making a very simple code. Most users should be able to do this, but time and time again I see tools that could easy implement this useful function, but don't.
So you can use this for a lot of things, such as:
Saving Login Credentials (You may want to make an Encryption script and Decryption Script for this)
Saving Program Config (EG: Colour, Themes, Fonts, Size, Checkboxes/Radiobuttons selected?!)
& anything you can see yourself using this for. Now, lets get into it!

First, make a new Form on VS (I'll be using VS 2010). For the sake of this tutorial, my Project is going to be called 'Tutorial'
Setup the Form's Setting how you like (or you can implement this into a pre-made form)
Make something you want to save. For me, this is going to be background colour. Ill be making 4 RadioButtons with 4 Different colours for me to save to a BackgroundConfig.txt (the .txt can be nearly ANYTHING you want it to be, eg: .CCM, .PFM, .Harry, .AllahuAkbar, etc. It is just reading & writing to it in plain text.

xcia3mT.png

Now lets make a new timer and double click on it so we can dive into the code :smile:
You want to paste this script (This will make it so you can actually write to the file)
Code:
private void timer1_Tick(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\Config.txt", "Colour = Crimson");
                this.BackColor = System.Drawing.Color.Crimson;
            }
            else
                if (radioButton2.Checked == true)
                {
                    System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\Config.txt", "Colour = Cyan");
                    this.BackColor = System.Drawing.Color.Cyan;
                }
                else
                    if (radioButton3.Checked == true)
                    {
                        System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\Config.txt", "Colour = Lime");
                        this.BackColor = System.Drawing.Color.Lime;
                    }
                    else
                        if (radioButton4.Checked == true)
                        {
                            System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\Config.txt", "Colour = Yellow");
                            this.BackColor = System.Drawing.Color.Yellow;
                        }
        }

Ok. So you may want to edit this code ey?
Look at this code:
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\Config.txt", "Colour = Lime");
There are a few things you can change here.
"Colour = Lime" is what will be written to the file. This is also what we will be reading later in the tutorial.
"@"C:\Users\Public\TestFolder\Config.txt"" is where its going to be written.
Think carefully when changing this. Remember that a lot of people aren't going to have:
"C:\Users\<Your Name>" because their PC is going to be named something differently. If you aren't going to release this, don't worry, but if you are, you are going to need to choose a spot that everyone has. You can find things like the Programs directory online to write to (I don't have it right now)

Now, add this whole script somewhere in your code:
Code:
void ColourOnLoad()
        {
            string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\Config.txt");
            if(text == "Colour = Crimson")
            {
                this.BackColor = System.Drawing.Color.Crimson;
                radioButton1.Checked = true;
            }
            else
            if (text == "Colour = Cyan")
            {
                this.BackColor = System.Drawing.Color.Cyan;
                radioButton2.Checked = true;
            }
            else
            if (text == "Colour = Lime")
            {
                this.BackColor = System.Drawing.Color.Lime;
                radioButton3.Checked = true;
            }
            else
            if (text == "Colour = Yellow")
            {
                this.BackColor = System.Drawing.Color.Yellow;
                radioButton4.Checked = true;
            }
            else
                if (text == "" || text == " ")
                {
                    MessageBox.Show("Config is broken", "Tool Name");
                }
        }
Double Click on the form and paste this:
Code:
ColourOnLoad();
            timer1.Start();

and now your good to go :smile:

Post questions below!

Now the only thing you need to remember, is your need to make the Config file itself and place it there :smile:

Have fun. You can edit this script to change and run anything you want.

~ Harry
 
Top