C# .Net 使用 Settings.settings 保存 ListView
C# .Net Save a ListView using Settings.settings
我正在尝试使用 C# .Net 保存和加载 ListView 列表的内容。我希望通过创建一个变量 System.Windows.Forms.ListView 然后用它填充它来保存它。
用于保存的代码片段:
Properties.Settings.Default.followedUsersSettings = followerList;
Properties.Settings.Default.Save();
加载代码段:
if (Properties.Settings.Default.followedUsersSettings != null) {
followerList = Properties.Settings.Default.followedUsersSettings;
}
我似乎无法使用该代码让它工作。有没有更好的方法来尽可能简单地保存它?该列表是单列的,因此数组应该也可以,但我不确定推荐的是什么。
if (Properties.Settings.Default.followedUsersSettingsListView == null)
{
// adding default items to settings
Properties.Settings.Default.followedUsersSettingsListView = new System.Collections.Specialized.StringCollection();
Properties.Settings.Default.followedUsersSettingsListView.AddRange(new string [] {"Item1", "Item2"});
Properties.Settings.Default.Save();
}
// load items from settings
followerList.Items.AddRange((from i in Properties.Settings.Default.followedUsersSettingsListView.Cast<string>()
select new ListViewItem(i)).ToArray());
好的,我成功了。
节省:
//Convert the listview to a normal list of strings
var followerList = new List<string>();
//add each listview item to a normal list
foreach (ListViewItem Item in followerListView.Items) {
followerList.Add(Item.Text.ToString());
}
//create string collection from list of strings
StringCollection collection = new StringCollection();
//set the collection setting (created in Settings.settings as a specialized collection)
Properties.Settings.Default.followedUsersSettingsCollection = collection;
//persist the settings
Properties.Settings.Default.Save();
加载:
//check for null (first run)
if (Properties.Settings.Default.followedUsersSettings != null) {
//create a new collection again
StringCollection collection = new StringCollection();
//set the collection from the settings variable
collection = Properties.Settings.Default.followedUsersSettingsCollection;
//convert the collection back to a list
List<string> followedList = collection.Cast<string>().ToList();
//populate the listview again from the new list
foreach (var item in followedList) {
followerListView.Items.Add(item);
}
}
希望这对通过 Google 搜索找到此内容的人有意义。
我正在尝试使用 C# .Net 保存和加载 ListView 列表的内容。我希望通过创建一个变量 System.Windows.Forms.ListView 然后用它填充它来保存它。
用于保存的代码片段:
Properties.Settings.Default.followedUsersSettings = followerList;
Properties.Settings.Default.Save();
加载代码段:
if (Properties.Settings.Default.followedUsersSettings != null) {
followerList = Properties.Settings.Default.followedUsersSettings;
}
我似乎无法使用该代码让它工作。有没有更好的方法来尽可能简单地保存它?该列表是单列的,因此数组应该也可以,但我不确定推荐的是什么。
if (Properties.Settings.Default.followedUsersSettingsListView == null)
{
// adding default items to settings
Properties.Settings.Default.followedUsersSettingsListView = new System.Collections.Specialized.StringCollection();
Properties.Settings.Default.followedUsersSettingsListView.AddRange(new string [] {"Item1", "Item2"});
Properties.Settings.Default.Save();
}
// load items from settings
followerList.Items.AddRange((from i in Properties.Settings.Default.followedUsersSettingsListView.Cast<string>()
select new ListViewItem(i)).ToArray());
好的,我成功了。
节省:
//Convert the listview to a normal list of strings
var followerList = new List<string>();
//add each listview item to a normal list
foreach (ListViewItem Item in followerListView.Items) {
followerList.Add(Item.Text.ToString());
}
//create string collection from list of strings
StringCollection collection = new StringCollection();
//set the collection setting (created in Settings.settings as a specialized collection)
Properties.Settings.Default.followedUsersSettingsCollection = collection;
//persist the settings
Properties.Settings.Default.Save();
加载:
//check for null (first run)
if (Properties.Settings.Default.followedUsersSettings != null) {
//create a new collection again
StringCollection collection = new StringCollection();
//set the collection from the settings variable
collection = Properties.Settings.Default.followedUsersSettingsCollection;
//convert the collection back to a list
List<string> followedList = collection.Cast<string>().ToList();
//populate the listview again from the new list
foreach (var item in followedList) {
followerListView.Items.Add(item);
}
}
希望这对通过 Google 搜索找到此内容的人有意义。