For my c# windows form application, I want to preserve some user data on the client machine. What should be my steps (eg. local database, xml files), which one would be most secured.
3 Answers
The built in way to handle this is the settings class.
In the project file there is even a 'settings' tab you can enter values for your app. Access them at run time like
Width = Settings1.Default.Width;
Height = Settings1.Default.Height;
Top = Settings1.Default.Top;
Left = Settings1.Default.Left;
Save() and Load() are also available.

- 337
- 6
- 18
The code I use for all my programs to load and save settings at startup and shutdown:
private void Window_Closing(object sender, CancelEventArgs e)
{
SaveAppSettings();
}
private void LoadAppSettings()
{
if (Properties.Settings.Default.LastWindowSize.Width > System.Windows.SystemParameters.WorkArea.Width)
this.Width = System.Windows.SystemParameters.WorkArea.Width - 1; // stay within screen resolution
else
this.Width = Properties.Settings.Default.LastWindowSize.Width;
if (Properties.Settings.Default.LastWindowSize.Height > System.Windows.SystemParameters.WorkArea.Height)
this.Height = System.Windows.SystemParameters.WorkArea.Height - 1; // stay within screen resolution
else
this.Height = Properties.Settings.Default.LastWindowSize.Height;
if (Properties.Settings.Default.LastWindowPos.X > 0) // keep it sane, otherwise window appears to disappear
this.Left = Properties.Settings.Default.LastWindowPos.X;
if(Properties.Settings.Default.LastWindowPos.Y > 0)
this.Top = Properties.Settings.Default.LastWindowPos.Y;
}
private void SaveAppSettings()
{
if (this.WindowState == WindowState.Normal)
{
Properties.Settings.Default.LastWindowPos = new System.Drawing.Point((int)this.Left, (int)this.Top);
Properties.Settings.Default.LastWindowSize = new System.Drawing.Size((int)this.Width, (int)this.Height);
}
else
{
Properties.Settings.Default.LastWindowPos
= new System.Drawing.Point((int)this.RestoreBounds.Left, (int)this.RestoreBounds.Top);
Properties.Settings.Default.LastWindowSize
= new System.Drawing.Size((int)this.RestoreBounds.Width, (int)this.RestoreBounds.Height);
}
Properties.Settings.Default.Save();
}
I put in some checks for rational sizing since I use VMWare Fusion. If the fusion window is smaller than the last application size you could get into a situation where you can no longer resize the application properly. This should give you an idea of one way to do things.

- 3,466
- 7
- 19
- 31
I would use App.config and access the settings using the ConfigurationManager.AppSettings class.
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="Setting1" value="true"/>
<add key="Setting2" value="value"/>
</appSettings>
</configuration>
Access settings from your application with the following
bool setting1 = Convert.ToBolean(ConfigurationManager.AppSettings["Setting1"]);
string setting2 = ConfigurationManager.AppSettings["Setting2"];
To update settings create a static helper method
public static void UpdateAppSetting(string key, string value)
{
var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
}
See the link below for more examples.

- 3,666
- 4
- 20
- 19
-
thanks it was helpful – DG_ Jun 07 '15 at 07:47