Saving application settings in the project folder, not AppData

I have a Settings.cs file in my project and I am accessing data from it from my program through

Properties.Settings.Default.MyProperty 

The generated settings file is saved in the following location

 C:\Users\Foo\AppData\Local\MyApp\MyApp.exe_Url_jknwq2raeohczydfp1loj02nf05zldfk\1.0.0.0\user.config 

The problem is that this is not only user-specific, but also causes the program to have many user.config files for each signature (debug / release, etc.), which forces the developer user to fill in all the settings again every time he launches a "version" of a program that does not yet have a specific user.config. (If I am not clear enough, I will be happy to provide more detailed information)

I would like my application to have uniform settings files for all users and regardless of the "version" (debug / release, or else). Thus, the developer will have to set the settings once, and these settings will be effective every time the application is launched without having to re-enter them for other signatures / users.

+3
source share
4 answers

Finally, I decided to use a simpler alternative, consisting in creating a simple settings class and serializing / deserializing it, as described here

0
source

Just use the settings with the scope.

+1
source

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; } 
+1
source

I developed a simple solution with some disadvantages:

 public void WriteLocalValue(string localKey, string curValue) { Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); KeyValueConfigurationElement k = config.AppSettings.Settings[localKey]; if (k == null) config.AppSettings.Settings.Add(localKey, curValue); else k.Value = curValue; config.Save(); } public string ReadLocalValue(string localKey, string defValue) { string v = defValue; try { Configuration config = ConfigurationManager.OpenExeConfiguration( Application.ExecutablePath); KeyValueConfigurationElement k = config.AppSettings.Settings[localKey]; if (k != null) v = (k.Value == null ? defValue : k.Value); return v; } catch { return defValue; } } 

Problem: You need UAC to be able to write over your executable configuration, and you cannot use the Properties.Settings.Default.MyProperty syntax.

+1
source

Source: https://habr.com/ru/post/909078/


All Articles