我如何通过使用foreach循环一列和另一个foreach循环第二列来填充C#列表视图,这样第二个循环就不会先重写
本文关键字:循环 foreach 列表 视图 重写 填充 第二个 何通过 一列 二列 另一个 | 更新日期: 2023-09-27 18:28:50
我正在为两个注册表项提取url和url时间。并希望在列表视图中显示它。使用to循环,我如何将listview第一列一个循环一个循环地填充,因为url和时间都在不同的注册表项中。。。。。
listViewCookies.Columns.Add("TYPED URL", 300);
listViewCookies.Columns.Add("TIME", 400);
string[] url = new string[2];
ListViewItem item;
using (RegistryKey rk = Registry.Users.OpenSubKey(strSID + @"'Software'Microsoft'Internet Explorer'TypedURLs"))
{
try
{
foreach (string u in rk.GetValueNames())
{
url[0] = rk.GetValue(u).ToString();
}
}
catch { }
}
using (RegistryKey rk = Registry.Users.OpenSubKey(strSID + @"'Software'Microsoft'Internet Explorer'TypedURLsTime"))
{
try
{
foreach (string u in rk.GetValueNames())
{
object val = rk.GetValue(u);
DateTime output = DateTime.MinValue;
if (val is byte[] && ((byte[])val).Length == 8)
{
byte[] bytes = (byte[])val;
System.Runtime.InteropServices.ComTypes.FILETIME ft = new System.Runtime.InteropServices.ComTypes.FILETIME();
int valLow = bytes[0] + 256 * (bytes[1] + 256 * (bytes[2] + 256 * bytes[3]));
int valTwo = bytes[4] + 256 * (bytes[5] + 256 * (bytes[6] + 256 * bytes[7]));
ft.dwLowDateTime = valLow;
ft.dwHighDateTime = valTwo;
DateTime UTC = DateTime.FromFileTimeUtc((((long)ft.dwHighDateTime) << 32) + ft.dwLowDateTime);
TimeZoneInfo lcl = TimeZoneInfo.Local;
TimeZoneInfo utc = TimeZoneInfo.Utc;
output = TimeZoneInfo.ConvertTime(UTC, utc, lcl);
url[1] = output.ToString();
}
}
}
catch { }
}
item = new ListViewItem(url);
listViewCookies.Items.Add(item);
在剥离不相关的代码后,您将得到以下内容:
string[] url = new string[2];
foreach (string u in someCollection)
{
url[0] = someValue(u);
}
foreach (string u in someOtherCollection)
{
url[1] = someOtherValue(u);
}
ListViewItem item = new ListViewItem(url);
listViewCookies.Items.Add(item);
此代码的表单不会向列表视图中插入列表项的集合,而是插入具有两个值的单个列表项。
第一个值在第一个循环中重复设置,第二个值在第二个循环中反复设置。您不断地覆盖同一个值,最后只剩下一对值。
你可以做的是:
//container for the first item of the pair
List<string> typedUrls = new List<string>();
foreach (string u in someCollection)
{
typedUrls.Add(someValue(u));
}
//container for the second item of the pair
List<string> times = new List<string>();
foreach (string u in someOtherCollection)
{
times.Add(someOtherValue(u));
}
//now loop the containers, and construct a string[] for each
//assuming that they have the exact same length
for (int i = 0; i < typedUrls.Count; i++)
{
//create a string[]
string[] stringItem = { typedUrls[i], times[i]};
//construct a ListViewItem
ListViewItem item = new ListViewItem(stringItem);
//add it to the listView
listViewCookies.Items.Add(item);
}
与其构建新的ListViewItem
,不如从一开始就构建它:
ListViewItem item = new ListViewItem();
然后,如果你想设置第一列:
item.Text = "url ..." // Column 0 (Url)
设置第二列:
item.SubItems.Add("time..."); // Column 1 (Time)
然后,在最后,将ListViewItem
添加到列表视图中:
listViewCookies.Items.Add(item);
编辑,这里有一个修改后的例子:
listViewCookies.Columns.Add("TYPED URL", 300);
listViewCookies.Columns.Add("TIME", 400);
ListViewItem item = new ListViewItem();
using (RegistryKey rk = Registry.Users.OpenSubKey(strSID + @"'Software'Microsoft'Internet Explorer'TypedURLs"))
{
try
{
foreach (string u in rk.GetValueNames())
{
item.Text = rk.GetValue(u).ToString();
}
}
catch { }
}
using (RegistryKey rk = Registry.Users.OpenSubKey(strSID + @"'Software'Microsoft'Internet Explorer'TypedURLsTime"))
{
try
{
foreach (string u in rk.GetValueNames())
{
object val = rk.GetValue(u);
DateTime output = DateTime.MinValue;
if (val is byte[] && ((byte[])val).Length == 8)
{
byte[] bytes = (byte[])val;
System.Runtime.InteropServices.ComTypes.FILETIME ft = new System.Runtime.InteropServices.ComTypes.FILETIME();
int valLow = bytes[0] + 256 * (bytes[1] + 256 * (bytes[2] + 256 * bytes[3]));
int valTwo = bytes[4] + 256 * (bytes[5] + 256 * (bytes[6] + 256 * bytes[7]));
ft.dwLowDateTime = valLow;
ft.dwHighDateTime = valTwo;
DateTime UTC = DateTime.FromFileTimeUtc((((long)ft.dwHighDateTime) << 32) + ft.dwLowDateTime);
TimeZoneInfo lcl = TimeZoneInfo.Local;
TimeZoneInfo utc = TimeZoneInfo.Utc;
output = TimeZoneInfo.ConvertTime(UTC, utc, lcl);
item.SubItems.Add(output.ToString());
}
}
}
catch { }
}
listViewCookies.Items.Add(item);
}