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

Toseef Ahmed Abbasi

I am extracting url and url time for two registry key. 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.....

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

Instead of constructing a new ListViewItem you can construct it at the beginning:

 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:

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

    }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I write an if statement using a variable in a foreach loop using Powershell?

분류에서Dev

break a matrix to sub-matrices with equal 2nd column without using for loop

분류에서Dev

How Can I Pull An Entire Result From A Foreach Loop

분류에서Dev

Why do i have to use a for loop and not a foreach loop?

분류에서Dev

How to pass a variable generated from foreach loop to another page in PHP

분류에서Dev

Write JavaScript code in MVC Razor foreach loop

분류에서Dev

tcsh: how can we display the body of a foreach loop in history

분류에서Dev

for-loop to foreach with negative (i--) 문

분류에서Dev

How can I foreach a hashmap?

분류에서Dev

Nested loop in iMacros (2nd loop)

분류에서Dev

Ignore Duplicates in Foreach loop

분류에서Dev

How do I loop over output from shuf?

분류에서Dev

How can I resolve this loop in c#?

분류에서Dev

PHP 2 Arrays in Foreach-loop?

분류에서Dev

How can I read ListView Column Headers and their values?

분류에서Dev

How can I use my laptop as a 2nd monitor?

분류에서Dev

How can I get 2nd digit of a number manually

분류에서Dev

In Python, how to write strings in a file in a loop and before the loop stops, I can get the results runtimely?

분류에서Dev

Why is the 2nd for loop is not running?

분류에서Dev

Adding Nodes To TreeView In For/ForEach Loop

분류에서Dev

Undefined variable outside a foreach loop

분류에서Dev

php foreach loop duplicate items

분류에서Dev

Laravel foreach loop with jquery slidetoggle

분류에서Dev

Modify XML in foreach loop in PHP

분류에서Dev

foreach loop not working with laravel queue

분류에서Dev

PHP email not echoing a foreach loop

분류에서Dev

Changing a foreach loop to an if statement for reading a text file C#

분류에서Dev

How to remove selected files listed in the the second column of Listview?

분류에서Dev

How to link different URLs to different items in forEach loop in JSP page?

Related 관련 기사

  1. 1

    How can I write an if statement using a variable in a foreach loop using Powershell?

  2. 2

    break a matrix to sub-matrices with equal 2nd column without using for loop

  3. 3

    How Can I Pull An Entire Result From A Foreach Loop

  4. 4

    Why do i have to use a for loop and not a foreach loop?

  5. 5

    How to pass a variable generated from foreach loop to another page in PHP

  6. 6

    Write JavaScript code in MVC Razor foreach loop

  7. 7

    tcsh: how can we display the body of a foreach loop in history

  8. 8

    for-loop to foreach with negative (i--) 문

  9. 9

    How can I foreach a hashmap?

  10. 10

    Nested loop in iMacros (2nd loop)

  11. 11

    Ignore Duplicates in Foreach loop

  12. 12

    How do I loop over output from shuf?

  13. 13

    How can I resolve this loop in c#?

  14. 14

    PHP 2 Arrays in Foreach-loop?

  15. 15

    How can I read ListView Column Headers and their values?

  16. 16

    How can I use my laptop as a 2nd monitor?

  17. 17

    How can I get 2nd digit of a number manually

  18. 18

    In Python, how to write strings in a file in a loop and before the loop stops, I can get the results runtimely?

  19. 19

    Why is the 2nd for loop is not running?

  20. 20

    Adding Nodes To TreeView In For/ForEach Loop

  21. 21

    Undefined variable outside a foreach loop

  22. 22

    php foreach loop duplicate items

  23. 23

    Laravel foreach loop with jquery slidetoggle

  24. 24

    Modify XML in foreach loop in PHP

  25. 25

    foreach loop not working with laravel queue

  26. 26

    PHP email not echoing a foreach loop

  27. 27

    Changing a foreach loop to an if statement for reading a text file C#

  28. 28

    How to remove selected files listed in the the second column of Listview?

  29. 29

    How to link different URLs to different items in forEach loop in JSP page?

뜨겁다태그

보관