提示:文中分为txt和注册表两种方式,二选一即可。
1.安装
txt
注册表
2.引用
txt
using ApeFree.DataStore;
using ApeFree.DataStore.Adapters;
using ApeFree.DataStore.Local;
注册表
using ApeFree.DataStore;
using ApeFree.DataStore.Core;
3.实现
创建对象
//Setting即为需要保存读取的数据
public class Settings
{
public string Text { get; set; }
public string[] strItems { get; set; } = new string[0];
}
txt
private LoaclStore<Settings> store;
注册表
private IStore<Setting> store;
实例化
txt
// ./settings.txt为文件保存路径
// {SerializationAdapter=new XmlSerializationAdapter()}是xml格式保存
store = StoreFactory.Factory.CreateLoaclStore<Settings>(new LoaclStoreAccessSettings("./settings.txt") { SerializationAdapter=new XmlSerializationAdapter()});
注册表
//@"ApeFree\DataStore\Demo" 为注册表路径
store = StoreFactory.Factory.CreateRegistryStore<Setting>(new ApeFree.DataStore.Registry.RegistryStoreAccessSettings(Microsoft.Win32.RegistryHive.CurrentUser, @"ApeFree\DataStore\Demo", "Setting"));
读取
store.Load();
textBox1.Text = store.Value.Text;
listBox1.Items.AddRange(store.Value.strItems);
保存
store.Value.Text = textBox1.Text;
store.Value.strItems = listBox1.Items.Cast<string>().ToArray();
store.Save();