You must Sign In to post a response.
  • Category: .NET

    Windows/Desktop application that automatically reads a CSV file from my local directory,c#

    I want to write a small windows/desktop application in C#, that basically works like this

    If I click a button i want it to read and loop through a csv file that is stored on my local computer folder.

    Your help will be greatly appreciated.
  • #768774
    Hi Spha,

    Refer below sample code.

    public DataTable ReadCSV(string inputPath)
    using (CsvReader csv = new CsvReader(new StreamReader(inputPath), true))
    dtCSV = ReadHeaders(csv);

    while (csv.ReadNextRecord())
    DataRow drcsv = dtCSV.NewRow();
    for (int i = 0; i < dtCSV.Columns.Count; i++)
    drcsv[i] = dtCSV.Columns[i].ToString();

    catch (Exception ex)
    return dtCSV;

    Read below article to know more about how to read csv file.

    Hope this helps you....

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #768775

    Hai Spha,
    As per your requirements, you can use the C# code to read the file using the delimiter which is inside the local folder.
    Once you read the file using line by line, you can get all the values and can stored them in the table/data-table.
    You can get the code snippet from the below links:

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #768777
    There are number of ways to read CSV file, you can use OLEDB reader or EXCEL Interop application to read csv file see below snippet

    var filename = @"c:\work\test.csv";
    var connString = string.Format(
    @"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""",
    using (var conn = new OleDbConnection(connString))
    var query = "SELECT * FROM [" + Path.GetFileName(filename) + "]";
    using (var adapter = new OleDbDataAdapter(query, conn))
    var ds = new DataSet("CSV File");

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • Sign In to post your comments