You can save and read settings, such as all advanced programs in the Registry , and here's how to do it:
public object GetRegistryValue(string KeyName, object DefaultValue) { object res = null; try { Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer(); Microsoft.Win32.RegistryKey k = c.Registry.CurrentUser.OpenSubKey("Software\\YourAppName", true); if (k != null) { res = k.GetValue(KeyName, DefaultValue); } else { k = c.Registry.CurrentUser.CreateSubKey("Software\\YourAppName"); } if (k != null) k.Close(); // ex As Exception } catch { //PromptMsg(ex) } return res; } public void SetRegistryValue(string KeyName, object _Value) { try { Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer(); Microsoft.Win32.RegistryKey k = c.Registry.CurrentUser.OpenSubKey("Software\\YourAppName", true); if (k != null) { k.SetValue(KeyName, _Value); } else { k = c.Registry.CurrentUser.CreateSubKey("Software\\YourAppName"); k.SetValue(KeyName, _Value); } if (k != null) k.Close(); // ex As Exception } catch { //PromptMsg(ex) } }
Another choice: you have a serializable class ( [Serializable ()] that contains all your settings as properties, and then save it in the application directory with the BinaryFormatter class.
public void saveBinary(object c, string filepath) { try { using (System.IO.Stream sr = System.IO.File.Open(filepath, System.IO.FileMode.Create)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(sr, c); sr.Close(); } } catch (Exception ex) { throw ex; } } public object loadBinary(string path) { try { if (System.IO.File.Exists(path)) { using (System.IO.Stream sr = System.IO.File.Open(path, System.IO.FileMode.Open)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); object c = bf.Deserialize(sr); sr.Close(); return c; } } else { throw new Exception("File not found"); } } catch (Exception ex) { throw ex; } return null; }
source share