Config Creator in C#

Today in C#, i will teach you how to create a program that has a config creator in C#. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add two Textboxes named textbox1 that holds the property name and textbox2 for the property value. Insert two buttons named Button1 that adds property and Button2 for saving config. 3. In your code view, put the following code: We need to import the System.IO namespace in order to access files within the computers filesystem for saving later on.
  1. using System.IO;
To hold all the current properties to write to the config file in the near future, we will create a String list within the global scope with this variable:
  1. Dim props As New List(Of String)
4. Put this code in your Button1_Click to ensure that the property and values are not empty.
  1.     If (Not TextBox1.Text = Nothing And Not TextBox2.Text = Nothing) Then
  2.      
  3.     Else
  4.      
  5.     End If
If they are both not nothing, we want to add the values using a delimiter of a colon (:) to separate the name from the value, to the list.
  1.     props.Add(TextBox1.Text + ":" + TextBox2.Text)
5. Put this code in your Button2_Click for saving the config file. This will allow the user to set a save destination through a new SaveFileDialog browser box, then use a new StreamWriter to write each property held within our 'props' list variable/object to the new config file.
  1. if (props.Count() > 0)
  2. {
  3.         using (OpenFileDialog fo = new OpenFileDialog())
  4.         {
  5.                 fo.MultiSelect = false;
  6.                 fo.RestoreDirectory = true;
  7.                 fo.ShowDialog();
  8.                 if (fo.FileName == null)
  9.                 {
  10.                         MessageBox.Show("That config name is not valid.");
  11.                 }
  12.                 else
  13.                 {
  14.                         using (StreamWriter sw = new StreamWriter(fo.FileName))
  15.                         {
  16.                                 foreach (string prop in props)
  17.                                 {
  18.                                         sw.WriteLine(prop);
  19.                                 }
  20.                         }
  21.                        
  22.                 }
  23.         }
  24.        
  25. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment