none
How to get csv files to for c# windows form application automatically without selecting RRS feed

  • Question

  • How to get csv files to c# windows form application automatically without selecting using link label option

    before i was selecting using link label for a search part now i want to do selecting the csv files automatically.

    how to do it like that

    Saturday, August 17, 2019 12:46 AM

All replies

  • Hello,

    Describe what you mean by automatically (nothing is automatic) as there has to be some type of logic along with what triggers said logic. Without this we can not help other than guess at what you are asking.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 17, 2019 2:33 AM
    Moderator
  •  private void btnSearch_Click_1(object sender, EventArgs e)
            {
                int rows = 0;
                string txtFileName = "";
                string[] files = Directory.GetFiles(@"D\", "*-CEB DATA.CSV", SearchOption.AllDirectories);
                DateTime starttime = dtpstartdate.Value.Add(dtpstarttime.Value.TimeOfDay);
                DateTime endtime = dtpenddate.Value.Add(dtpendtime.Value.TimeOfDay);
                DateTime oDate = DateTime.Now;
                DateTime oDate2 = DateTime.Now;
    
                string filename = "";
                for (int i = 0; i < files.Length; i++)
                {
                    txtFileName = files[i].ToString();
                    int index1 = txtFileName.LastIndexOf('\\');
                    if (index1 != -1)
                    {
                        filename = txtFileName.Substring(index1 + 1);
                        index1 = filename.LastIndexOf('-');
                        if (index1 >= 0)
                        {
                            filename = filename.Substring(0, index1);
                            try
                            {
                                oDate = DateTime.ParseExact(filename, "d-M-yyyy", null);
                                TimeSpan ts = new TimeSpan(23, 59, 59);
                                oDate2 = oDate + ts;
                            }
                            finally
                            {
                                if (oDate2 >= starttime && oDate <= endtime)
                                {
                                    GetDataFromFile(txtFileName, starttime, endtime, ref rows);
                                }
                            }
    
                        }
                    }
                }
                if (rows > 0)
                {
                    Cursor = Cursors.WaitCursor;
                    SaveImportDataToDatabase(dataGridView1, rows);
                    Cursor = Cursors.Default;
                }

    Normally the above code only i used to get data by now i am not able get data by csv file name is 20190625.csv

    how can i get this file data automatically when i am doing a search option in my program

    Saturday, August 17, 2019 3:19 AM
  • Hi tharminianandasivanesan,

    Thank you for posting here.

    Based on your description, you want to get csv files automatically.

    Please try the following change:

    Change:

     string[] files = Directory.GetFiles(@"D\", "*-CEB DATA.CSV", SearchOption.AllDirectories);

    Into:

    var files = Directory.GetFiles("D:\\", "*.csv", SearchOption.AllDirectories);

    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.

    Monday, August 19, 2019 3:27 AM
    Moderator
  • can i use "*-20190625.CSV" instead of this "*-CEB DATA.CSV"

    Monday, August 19, 2019 4:19 AM
  • Hi tharminianandasivanesan,

    Thanks for the feedback.

    >>can i use "*-20190625.CSV" instead of this "*-CEB DATA.CSV"

    I am not sure what you mean. As usual, GetFiles(String, String, SearchOption) method's second parameter means that the search string to match against the names of files in path.

    For example, We need to select all the .txt files. We will use the following code.

    var files = Directory.GetFiles("D:\\", "*.txt", SearchOption.AllDirectories);

    I hope that you could undertsand it.

    By the way,  this thread has been solved, you could mark the appropriate reply as answer.

    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.



    Monday, August 19, 2019 5:47 AM
    Moderator