'how to import items from txt to listview c# [closed]

I'm new at C# still learning I'm coding a bulk mail sender application, I want to import mail address from txt file to list view for example

  No | Email Address |
  1    [email protected]
  2    [email protected]
  3    [email protected]
  4    [email protected]
  5    [email protected]

I not typed any code for this because Idk how to do that but I explained what I want to do I'll add a photo too

enter image description here

I need a sample code, when I click import open a txt file and import email addresses to "Email Address" Columns

Thanks



Solution 1:[1]

You can try quering the file; assuming that UI is WinForms you can put something like this:

using System.IO;
using System.Linq;

...

var itemsToAdd = File
  .ReadLines(@"c:\ItemsToCreate.txt")
  .Skip(1)                            // Skip Caption   
  .Select(line => line.Split(         // Split each line 
        new char[] { ' ', '\t' },     // .. by tabulations and spaces
        StringSplitOptions.RemoveEmptyEntries) // .. while dropping empty chunks
     .LastOrDefault())                // take the last chunk (or null) from the split    
  .Where(email => !string.IsNullOrEmpty(email)) // filter out null or empty emails 
  .Select(email => new ListViewItem(email)) // create a ListViewItem from email
  .ToArray();                         // materialize list view items into an array 

listView1.Items.AddRange(itemsToAdd);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1