2

我正在为两个注册表项提取 url 和 url 时间。并希望在列表视图中显示它。使用 to 循环我如何通过循环填充 listview 第一列,然后再填充第二列,因为 url 和 time 都在不同的 reg 键中.....

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);
4

2 回答 2

2

剥离不相关的代码后,您会得到以下内容:

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);
}
于 2013-08-23T13:19:34.417 回答
1

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);

    }
于 2013-08-23T13:00:27.777 回答