简体   繁体   English

我如何通过使用foreach循环一列和另一个foreach填充第二列来填充C#列表视图,以便第二个循环不会首先覆盖

[英]How can i fill a C# listview by using foreach loop a column and another foreach for second column so that 2nd loop do not over write first

I am extracting url and url time for two registry key. 我正在提取两个注册表项的url和url时间。 And wants to show it in list view. 并希望在列表视图中显示它。 Using to loops how can i fill listview first column by a loop an second by another because both url and time is in diffrent reg keys..... 使用to循环,我如何才能一次又一次地循环填充listview第一列,因为url和时间都在不同的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);

After stripping the unrelated code you are left with something like: 剥离不相关的代码后,您将得到以下内容:

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

The form of this code does not insert a collection of list items to a list view, instead it inserts a single list item with two values. 该代码的形式不向列表视图插入列表项的集合,而是插入具有两个值的单个列表项。

The first value is set repeatedly in the first loop, and the second in the second loop. 在第一个循环中重复设置第一个值,在第二个循环中重复设置第二个值。 You are constantly overwriting the same value, and at the end you are left with a single pair of values. 您不断地覆盖相同的值,最后只剩下一对值。

What you could do is something like: 您可以做的是:

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

Instead of constructing a new ListViewItem you can construct it at the beginning: 您可以在开始时构造它,而不是构造一个新的ListViewItem

 ListViewItem item = new ListViewItem();

Then if you want to set the first column: 然后,如果要设置第一列:

 item.Text = "url ..." // Column 0 (Url)

To set the second column: 设置第二列:

 item.SubItems.Add("time..."); // Column 1 (Time)

Then, at the end, add the ListViewItem to the list view: 然后,最后,将ListViewItem添加到列表视图:

 listViewCookies.Items.Add(item);

Edit, here's a modified example: 编辑,这是一个修改后的示例:

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM