select csv file in csharp

You can select a CSV file in C# using the OpenFileDialog class from the System.Windows.Forms namespace. Here's an example:

main.cs
using System.Windows.Forms;

// ...

var openFileDialog = new OpenFileDialog {
    Filter = "CSV Files (*.csv)|*.csv",
    Title = "Select a CSV File"
};

if (openFileDialog.ShowDialog() == DialogResult.OK) {
    var filePath = openFileDialog.FileName;
    using (var streamReader = new StreamReader(filePath)) {
        // Process the CSV file
    }
}
353 chars
16 lines

The Filter property of the OpenFileDialog specifies that only files with extension .csv will be visible in the dialog. The Title property sets the title of the dialog box that appears when selecting a CSV file.

Upon selecting a valid CSV file, the FilePath property of the OpenFileDialog object contains the full path. StreamReader is then used to read the contents of the selected file into memory. You can then process the data as necessary.

gistlibby LogSnag