I try some methods to store my settings to simply text file and i found best way:
file stored in application folder, to usage , settings.txt:
(inside settings file approved comments, try //comment)
//to get settings value
Settings.Get("name", "Ivan");
//to set settings value
Settings.Set("name", "John");
using:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
//you can store also with section name, to use just add name section Set(section_name,name,value) and Get(section_name,name,value)
public static class Settings
{
private static string SECTION = typeof(Settings).Namespace;//"SETTINGS";
private static string settingsPath = Application.StartupPath.ToString() + "\\settings.txt";
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public static String GetString(String name)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(SECTION,name,"",temp,255,settingsPath);
return temp.ToString();
}
public static String Get(String name, String defVal)
{
return Get(SECTION,name,defVal);
}
public static String Get(string _SECTION, String name, String defVal)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(_SECTION, name, "", temp, 255, settingsPath);
return temp.ToString();
}
public static Boolean Get(String name, Boolean defVal)
{
return Get(SECTION, name, defVal);
}
public static Boolean Get(string _SECTION, String name, Boolean defVal)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(_SECTION,name,"",temp,255,settingsPath);
bool retval=false;
if (bool.TryParse(temp.ToString(),out retval))
{
return retval;
} else
{
return retval;
}
}
public static int Get(String name, int defVal)
{
return Get(SECTION, name, defVal);
}
public static int Get(string _SECTION, String name, int defVal)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(SECTION,name,"",temp,255,settingsPath);
int retval=0;
if (int.TryParse(temp.ToString(),out retval))
{
return retval;
} else
{
return retval;
}
}
public static void Set(String name, String val)
{
Set(SECTION, name,val);
}
public static void Set(string _SECTION, String name, String val)
{
WritePrivateProfileString(_SECTION, name, val, settingsPath);
}
public static void Set(String name, Boolean val)
{
Set(SECTION, name, val);
}
public static void Set(string _SECTION, String name, Boolean val)
{
WritePrivateProfileString(_SECTION, name, val.ToString(), settingsPath);
}
public static void Set(String name, int val)
{
Set(SECTION, name, val);
}
public static void Set(string _SECTION,String name, int val)
{
WritePrivateProfileString(SECTION, name, val.ToString(), settingsPath);
}
}