I have a Settings.cs file in my project, and I access the data in it from my program via
Properties.Settings.Default.MyProperty
The generated settings file is stored 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 it also results in the program having many user.config files for every signature (debug/release, etc.), which forces the developer-user to populate the whole settings again each time he launches a "version" of the program that does not have a specific user.config yet. (If I am not being clear enough, I'll be glad to give more details)
I would like my application to have a single settings files for all users and no matter the "version" (debug/release, or else). This way, the dev-user would have to set the settings one single time and these settings would be effective each time the application is launched, without the need to re-enter them for the other signatures/users.
You can save and read setting like all advanced programs in Registry, and that is 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 that you make a serializable class ([Serializable()] attrib) that contains all of your settings as properties, then save it in your app 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With