C# C.Net使用Settings.Settings保存列表视图
我正在尝试使用C.Net保存和加载ListView列表的内容。我希望通过创建变量System.Windows.Forms.ListView来保存它,然后用它填充它 用于保存的代码段:C# C.Net使用Settings.Settings保存列表视图,c#,.net,listview,persistent-storage,C#,.net,Listview,Persistent Storage,我正在尝试使用C.Net保存和加载ListView列表的内容。我希望通过创建变量System.Windows.Forms.ListView来保存它,然后用它填充它 用于保存的代码段: Properties.Settings.Default.followedUsersSettings = followerList; Properties.Settings.Default.Save(); 用于加载的代码段: if (Properties.Settings.Default.followedUsers
Properties.Settings.Default.followedUsersSettings = followerList;
Properties.Settings.Default.Save();
用于加载的代码段:
if (Properties.Settings.Default.followedUsersSettings != null) {
followerList = Properties.Settings.Default.followedUsersSettings;
}
//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);
}
}
我似乎无法使用该代码使其工作。有没有更好的方法尽可能简单地保存这些信息?列表是单列的,所以数组也应该可以工作,但我不确定推荐什么。好的,我设法让它工作了
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();
装载时:
if (Properties.Settings.Default.followedUsersSettings != null) {
followerList = Properties.Settings.Default.followedUsersSettings;
}
//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);
}
}
希望这对在谷歌搜索中找到它的人来说是有意义的。好的,我设法让它工作了 保存:
//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();
装载时:
if (Properties.Settings.Default.followedUsersSettings != null) {
followerList = Properties.Settings.Default.followedUsersSettings;
}
//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);
}
}
希望这对在谷歌搜索中发现这一点的人来说是有意义的。什么是followerList?内容是什么?只是字符串列表还是有子项?@TaW内容只是使用System.Windows.Forms.Listview的简单字符串列表。没有子项什么是followerList?内容是什么?只是字符串列表还是有子项?@TaW内容只是使用System.Windows.Forms.Listview的简单字符串列表。没有子项,可能应该更好地命名我的变量。followerList是表单上显示的列表视图。FollowedUserSettingsListView是设置中使用的变量的名称。SettingsOry可能应该更好地命名我的变量。followerList是表单上显示的列表视图。FollowedUserSettingsListView是设置中使用的变量的名称。Settings但是当集合变量为空时,您不安全,是吗?但是当集合变量为空时,您不安全,是吗?