none
reading multiple files RRS feed

  • Question

  • Hi,

    I have 3 CSV files of format:

    File1

    Test_name

    test1

    test2

    File2

    Test_name

    Test1

    Test2

    File3

    Test_name

    Test3

    Test4

    Now I'm multi selecting these files and displaying in the listbox. (I have made selectionmode to none for the listbox)

    I want to select these multiple files which are displayed in the listbox and the testnames are fetched to the checkedlistbox which is in another form ignoring the duplicate testname. For example i need to display Test1,Test2,Test3,Test4 in the checkedlistbox for the above example.

    .



    Akshay

    Friday, May 3, 2019 6:49 AM

Answers

  • Hi

    Thanks for the feedback.

    I have modified my code, you could try the following code.

    Form1

     private void Button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfiledialog1 = new OpenFileDialog();
                openfiledialog1.Multiselect = true;
                openfiledialog1.InitialDirectory = "d:\\cvs\\";
                if (openfiledialog1.ShowDialog() == DialogResult.OK)
                {
                    for (int fi = 0; fi < openfiledialog1.FileNames.Length; fi++)
                    {
                        listBox1.Items.Add(openfiledialog1.FileNames[fi].ToString());
                    }
                }
            }
            DataTable dt = new DataTable();
            public  static List<string> li = new List<string>();
            private void Button2_Click(object sender, EventArgs e)
            {
                Form2 form = new Form2();
                form.Show();
                foreach (string item in listBox1.Items)
                {
                    li.Add(item);
                   
                }
               
                
            }

    Form2

    DataTable dt;
            private void Button1_Click(object sender, EventArgs e)
            {
                List<String> list1 = new List<string>();
                list1 = Form1.li;
                foreach (string item in list1)
                {
                    dt = OpenCSV(item);
                    foreach (DataRow dr in dt.Rows)
                    {
                        checkedListBox1.Items.Add(dr["Test_name"]);
                    }
                }
                List<string> list = new List<string>();
                foreach (var item in checkedListBox1.Items)
                {
                    list.Add(item.ToString());
                }
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list.Count; j++)
                    {
                        if ((list[i].ToUpper() == list[j].ToUpper()) && (i != j) && (list[i].StartsWith("t")))
                        {
                            checkedListBox1.Items.Remove(list[i]);
                        }
                    }
                }
            }
            public static DataTable OpenCSV(string filePath)
            {
                DataTable dt = new DataTable();
                FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    
                StreamReader sr = new StreamReader(fs);
                string strLine = "";
                string[] aryLine = null;
                string[] tableHead = null;
                int columnCount = 0;
                bool IsFirst = true;
                while ((strLine = sr.ReadLine()) != null)
                {
                    if (IsFirst == true)
                    {
                        tableHead = strLine.Split(',');
                        IsFirst = false;
                        columnCount = tableHead.Length;
                        for (int i = 0; i < columnCount; i++)
                        {
                            DataColumn dc = new DataColumn(tableHead[i]);
                            dt.Columns.Add(dc);
                        }
                    }
                    else
                    {
                        aryLine = strLine.Split(',');
                        DataRow dr = dt.NewRow();
                        for (int j = 0; j < columnCount; j++)
                        {
                            dr[j] = aryLine[j];
                        }
                        dt.Rows.Add(dr);
                    }
                }
                if (aryLine != null && aryLine.Length > 0)
                {
                    dt.DefaultView.Sort = tableHead[0] + " " + "asc";
                }
                sr.Close();
                fs.Close();
                return dt;
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 3, 2019 9:51 AM
    Moderator

All replies

  • Hello,

    To ignoring the duplicate testname you can add the NuGet package "System.Linq" in "Manage NuGet packages..." in Visual Studio.

    Add the reference

    using System.Linq;

    So, you should have a list of selected files. So now you can ignore the duplicate using .Distinct():

    var files = selectedFiles.Distinct();


    Friday, May 3, 2019 8:21 AM
  • Hi anonymous_3210,

    Thank you  for posting here.

    According to your description, you could try the following code.

      private void Button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfiledialog1 = new OpenFileDialog();
                openfiledialog1.Multiselect = true;
                openfiledialog1.InitialDirectory = "d:\\cvs\\";
                if (openfiledialog1.ShowDialog() == DialogResult.OK)
                {
                    for (int fi = 0; fi < openfiledialog1.FileNames.Length; fi++)
                    {
                        listBox1.Items.Add(openfiledialog1.FileNames[fi].ToString());
                    }
                }
            }
            DataTable dt = new DataTable();
            private void Button2_Click(object sender, EventArgs e)
            {
                foreach (string item in listBox1.Items)
                {
                    dt = OpenCSV(item);
                    foreach (DataRow dr in dt.Rows)
                    {
                        checkedListBox1.Items.Add(dr["Test_name"]);
                    }
                }
                List<string> list = new List<string>();
                foreach (var item in checkedListBox1.Items)
                {
                    list.Add(item.ToString());
                }
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list.Count; j++)
                    {
                        if((list[i].ToUpper()==list[j].ToUpper())&&(i!=j)&&(list[i].StartsWith("t")))
                        {
                            checkedListBox1.Items.Remove(list[i]);
                        }
                    }
                }
                
            }
    
            public static DataTable OpenCSV(string filePath)
            {
                DataTable dt = new DataTable();
                FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    
                StreamReader sr = new StreamReader(fs);
                string strLine = "";
                string[] aryLine = null;
                string[] tableHead = null;
                int columnCount = 0;
                bool IsFirst = true;
                while ((strLine = sr.ReadLine()) != null)
                {
                    if (IsFirst == true)
                    {
                        tableHead = strLine.Split(',');
                        IsFirst = false;
                        columnCount = tableHead.Length;
                        for (int i = 0; i < columnCount; i++)
                        {
                            DataColumn dc = new DataColumn(tableHead[i]);
                            dt.Columns.Add(dc);
                        }
                    }
                    else
                    {
                        aryLine = strLine.Split(',');
                        DataRow dr = dt.NewRow();
                        for (int j = 0; j < columnCount; j++)
                        {
                            dr[j] = aryLine[j];
                        }
                        dt.Rows.Add(dr);
                    }
                }
                if (aryLine != null && aryLine.Length > 0)
                {
                    dt.DefaultView.Sort = tableHead[0] + " " + "asc";
                }
                sr.Close();
                fs.Close();
                return dt;
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 3, 2019 8:22 AM
    Moderator
  • Hi,

    Thank you so much .

    I wanted to know what if i had that checkedlistbox in another form


    Akshay

    Friday, May 3, 2019 9:17 AM
  • Hi

    Thanks for the feedback.

    I have modified my code, you could try the following code.

    Form1

     private void Button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfiledialog1 = new OpenFileDialog();
                openfiledialog1.Multiselect = true;
                openfiledialog1.InitialDirectory = "d:\\cvs\\";
                if (openfiledialog1.ShowDialog() == DialogResult.OK)
                {
                    for (int fi = 0; fi < openfiledialog1.FileNames.Length; fi++)
                    {
                        listBox1.Items.Add(openfiledialog1.FileNames[fi].ToString());
                    }
                }
            }
            DataTable dt = new DataTable();
            public  static List<string> li = new List<string>();
            private void Button2_Click(object sender, EventArgs e)
            {
                Form2 form = new Form2();
                form.Show();
                foreach (string item in listBox1.Items)
                {
                    li.Add(item);
                   
                }
               
                
            }

    Form2

    DataTable dt;
            private void Button1_Click(object sender, EventArgs e)
            {
                List<String> list1 = new List<string>();
                list1 = Form1.li;
                foreach (string item in list1)
                {
                    dt = OpenCSV(item);
                    foreach (DataRow dr in dt.Rows)
                    {
                        checkedListBox1.Items.Add(dr["Test_name"]);
                    }
                }
                List<string> list = new List<string>();
                foreach (var item in checkedListBox1.Items)
                {
                    list.Add(item.ToString());
                }
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list.Count; j++)
                    {
                        if ((list[i].ToUpper() == list[j].ToUpper()) && (i != j) && (list[i].StartsWith("t")))
                        {
                            checkedListBox1.Items.Remove(list[i]);
                        }
                    }
                }
            }
            public static DataTable OpenCSV(string filePath)
            {
                DataTable dt = new DataTable();
                FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    
                StreamReader sr = new StreamReader(fs);
                string strLine = "";
                string[] aryLine = null;
                string[] tableHead = null;
                int columnCount = 0;
                bool IsFirst = true;
                while ((strLine = sr.ReadLine()) != null)
                {
                    if (IsFirst == true)
                    {
                        tableHead = strLine.Split(',');
                        IsFirst = false;
                        columnCount = tableHead.Length;
                        for (int i = 0; i < columnCount; i++)
                        {
                            DataColumn dc = new DataColumn(tableHead[i]);
                            dt.Columns.Add(dc);
                        }
                    }
                    else
                    {
                        aryLine = strLine.Split(',');
                        DataRow dr = dt.NewRow();
                        for (int j = 0; j < columnCount; j++)
                        {
                            dr[j] = aryLine[j];
                        }
                        dt.Rows.Add(dr);
                    }
                }
                if (aryLine != null && aryLine.Length > 0)
                {
                    dt.DefaultView.Sort = tableHead[0] + " " + "asc";
                }
                sr.Close();
                fs.Close();
                return dt;
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 3, 2019 9:51 AM
    Moderator
  • Thank you so much.

    That worked


    Akshay

    Friday, May 3, 2019 11:20 AM