简体   繁体   中英

Storing Settings for C#?

I have looked at the other posts, but this question is different than them.

I want to store 3 values of data (Type, String, String), how would I be able to do this?

An example of it would be: String, "lol", "lol2".

I've tried both the resource settings and settings settings, but neither work. They both say the name can't be doubled.

This is for a Windows Form

Any help would be appreciated thanks.

And I HAVE to HAVE it in this order. Because when someone selects the method "String" from the drop-down list I have, it shows everything that has the type of String including "lol". And then lol2 should be put in a textbox after selecting the 'lol" string.

Search for the IsolatedStorage class. This one is sux =). But if you are using Silverlight, then you may use IsolatedStorageSettings class which is awesome!

Settings.settings works. However it sounds like you would be better off making your own serialization format for this type of data. Keep in mind that the Settings.settings class is good for trivial user and application settings. Nested, complex, or large structures that can change should use an application specific format. Use settings.settings for window locations and color preferences (as an example).

Another option would be to create and manage your own settings file. Here is a sample to get you started. First you create a class to represent the data you want to store, CustomDataStoreItem. Then create another class to hold a collection of those CustomDataStoreItems:

[Serializable()]
class CustomDataStore
{
    public CustomDataStore()
    {
        //This is required by serialization
    }
    private List<CustomDataStoreItem> _items;

    public List<CustomDataStoreItem> Items
    {
        get
        {
            if (_items == null)
                _items = new List<CustomDataStoreItem>();
            return _items;
        }
        set { _items = value; }
    }

    public void LoadSettings(string SettingsFileName)
    {
        if (File.Exists(SettingsFileName))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = File.OpenRead(SettingsFileName);
            CustomDataStore ds = (CustomDataStore)bf.Deserialize(fs);
            fs.Close();
            _items = ds.Items;
        }
    }
    public void SaveSettings(string SettingsFileName)
    {
        if (File.Exists(SettingsFileName))
            File.Delete(SettingsFileName);
        BinaryFormatter bf = new BinaryFormatter();
        FileStream fs = File.Create(SettingsFileName);
        bf.Serialize(fs, this);
        fs.Close();
    }
}
[Serializable()]
class CustomDataStoreItem
{
    public CustomDataStoreItem()
    {
        //This is required by serialization
    }
    public string Type { get; set; }
    public string String1 { get; set; }
    public string String2 { get; set; }
}

Now you can make use of this class like so:

To save settings add CustomDataStoreItems to the CustomDataStore.Items collection. Then call the SaveSettings method:

CustomDataStore ds = new CustomDataStore();
CustomDataStoreItem dsi = new CustomDataStoreItem();
dsi.Type = "String";
dsi.String1 = "lol";
dsi.String2 = "lol2";
ds.Items.Add(dsi);
ds.SaveSettings(@"C:\Users\Public\Test.bin");

You can load the settings back out like this:

CustomDataStore ds = new CustomDataStore();
ds.LoadSettings(@"C:\Users\Public\Test.bin");
foreach (CustomDataStoreItem dsi in ds.Items)
 {
  Console.WriteLine(string.Format("{0} {1} {2}", dsi.Type, dsi.String1, dsi.String2));
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM