Link to home
Start Free TrialLog in
Avatar of NewMacAdmin
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
Avatar of ericpeckham
ericpeckham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NewMacAdmin
NewMacAdmin

ASKER

Where will that settings file be placed, what will it's name/file extension be?  Can I encrypt it to prevent the data from being tampered with?
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::WindowsFormsApplication1.Properties.Settings.Default.SaveMeValue;
                  this.checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::WindowsFormsApplication1.Properties.Settings.Default, "SaveMeValue", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
                  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.UseVisualStyleBackColor = true;

Here's the code VS generated for the setting to be saved (from Properties\Settings.Designer.cs):

namespace WindowsFormsApplication1.Properties {
   
   
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
       
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
       
        public static Settings Default {
            get {
                return defaultInstance;
            }
        }
       
        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("False")]
        public bool SaveMeValue {
            get {
                return ((bool)(this["SaveMeValue"]));
            }
            set {
                this["SaveMeValue"] = value;
            }
        }
    }
}

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.exe.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\Local\Microsoft\WindowsFormsApplication1._Url_31h2hba23faolvxzzag45mwubsv0qqk3\1.0.0.0\user.config.  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.
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Éric Moreau
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