NewMacAdmin
asked on
C# Code To Store Settings
What would be some example code to store data for future use in a C# 2010 program? (ie: check boxes on a form left checked, or unchecked, and text that was left in a field, so when reponening the program, the text is still there) Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I assumed you were using Visual Studio with a WinForms application. Here's the code that the designer generated for the checkbox (from Form1.Designer.cs):
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = global::WindowsFormsApplic ation1.Pro perties.Se ttings.Def ault.SaveM eValue;
this.checkBox1.DataBinding s.Add(new System.Windows.Forms.Bindi ng("Checke d", global::WindowsFormsApplic ation1.Pro perties.Se ttings.Def ault, "SaveMeValue", true, System.Windows.Forms.DataS ourceUpdat eMode.OnPr opertyChan ged));
this.checkBox1.Location = new System.Drawing.Point(27, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(69, 17);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "Save Me";
this.checkBox1.UseVisualSt yleBackCol or = true;
Here's the code VS generated for the setting to be saved (from Properties\Settings.Design er.cs):
namespace WindowsFormsApplication1.P roperties {
[global::System.Runtime.Co mpilerServ ices.Compi lerGenerat edAttribut e()]
[global::System.CodeDom.Co mpiler.Gen eratedCode Attribute( "Microsoft .VisualStu dio.Editor s.Settings Designer.S ettingsSin gleFileGen erator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configurati on.Applica tionSettin gsBase {
private static Settings defaultInstance = ((Settings)(global::System .Configura tion.Appli cationSett ingsBase.S ynchronize d(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configurat ion.UserSc opedSettin gAttribute ()]
[global::System.Diagnostic s.Debugger NonUserCod eAttribute ()]
[global::System.Configurat ion.Defaul tSettingVa lueAttribu te("False" )]
public bool SaveMeValue {
get {
return ((bool)(this["SaveMeValue" ]));
}
set {
this["SaveMeValue"] = value;
}
}
}
}
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = global::WindowsFormsApplic
this.checkBox1.DataBinding
this.checkBox1.Location = new System.Drawing.Point(27, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(69, 17);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "Save Me";
this.checkBox1.UseVisualSt
Here's the code VS generated for the setting to be saved (from Properties\Settings.Design
namespace WindowsFormsApplication1.P
[global::System.Runtime.Co
[global::System.CodeDom.Co
internal sealed partial class Settings : global::System.Configurati
private static Settings defaultInstance = ((Settings)(global::System
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configurat
[global::System.Diagnostic
[global::System.Configurat
public bool SaveMeValue {
get {
return ((bool)(this["SaveMeValue"
}
set {
this["SaveMeValue"] = value;
}
}
}
}
ASKER
It doesn't seem to five much option for encrypting the settings file. Is there a manual way to save settings such as text in a txtbox or a checkbox check status?
Here's a brief description of how user settings files work.
When VS builds the app, it places a file containing default application settings in the same directory as the application - in this case the file is named WindowsFormsApplication1.e xe.config.
When the settings are first saved (in this case the first time the window is closed), .NET creates a file under the user's AppData directory, which looks a lot like the .config file above, except it has the actual saved settings instead of the default settings. On my computer, it was saved at C:\Users\eric\AppData\Loca l\Microsof t\WindowsF ormsApplic ation1._Ur l_31h2hba2 3faolvxzza g45mwubsv0 qqk3\1.0.0 .0\user.co nfig. If this file exists when the application first looks for a value, it will look to see if this file exists, and if so, it loads those settings. If it's not there, it uses the default settings in the app directory.
I think there may be a way to encrypt this data, although you normally shouldn't need to worry about that as the user area of a hard drive is usually off limits to other users. If you need to encrypt sensitive data, you will need to do that in your code before it's saved, and then decrypt it when it is being loaded.
When VS builds the app, it places a file containing default application settings in the same directory as the application - in this case the file is named WindowsFormsApplication1.e
When the settings are first saved (in this case the first time the window is closed), .NET creates a file under the user's AppData directory, which looks a lot like the .config file above, except it has the actual saved settings instead of the default settings. On my computer, it was saved at C:\Users\eric\AppData\Loca
I think there may be a way to encrypt this data, although you normally shouldn't need to worry about that as the user area of a hard drive is usually off limits to other users. If you need to encrypt sensitive data, you will need to do that in your code before it's saved, and then decrypt it when it is being loaded.
ASKER
How can I do that in my code then? Some of the data will be sensitive. Thanks
If you are storing sensitive data, you should probably be using a database like SQL Server or Oracle.
nbove makes a good point. We could probably be more helpful if you were to explain exactly what you need to do. I would appreciate it if you would accept my answer, as I have answered your original question. This encryption thing should probably be another question, and I would be glad to help with that.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am simply storing a checkbox to determine if a string should be saved. The string is a seed to generate a unique password for a machine (from a previously asked question of mine). The encryption is just to protect the seed.
ericpeckham is correct in that he did answer my original question. I will however award a few points for the answer to my follow up question. Thanks
ericpeckham is correct in that he did answer my original question. I will however award a few points for the answer to my follow up question. Thanks
ASKER