How to save settings to an internal config in visual C# (.NET)

In this tutorial I teach you how to save settings to an internal config!

Saving settings to a config is really easy in C#, so let’s begin the tutorial!

1. Right-click on the project root in the Solution Explorer:


2. Select “Properties” from the list:


3.  You should see something similar to this:


4. Set the names, types, and default values to whatever you like. DO NOT change the scope to “Application” if you plan on being able to edit the settings from the code.

5. Getting and Setting the property:
    Getting:  [Variable Type] [Variable Name] = Properties.Settings.Default.[Name of Property];

    Setting:  Properties.Settings.Default.[Name of Property] = [String, Int, etc. (As long as it is the same type as the property)];


In this example, I am checking if the config property LastSaveDirectory is not empty. If it is not empty, set the SaveFileDialog‘s Initial Directory to the stored property.



How to loop through all Properties and display them in a MessageBox:


NOTE: I would recommend a String Builder if you plan on having a lot of properties.

write twice, method once

My brother Genji always says, “Measure twice, cut once”. After hearing him say this enough, I tried to apply it to my life and hobbies, and realized that in programming it’s the opposite. You don’t want to reuse code if you don’t have to. Let me show the full title now. Never write twice, method once. I realize that looks weird now, but the point still stands. If you have to use a piece of code more than once, you should probably make a method/function for it.
Let’s look at an example

Say you wanted to print out the contents of an array at multiple points within your program. Instead of having multiple for loops, you could just create one function, like this.

public void printArray(string[] myArray)
foreach (string s in myArray)