Search code examples
c#openfiledialog

using open file dialog box on a windows form


I wrote a program which reads a csv file, makes some changes and writes to a new csv file.

I want the user to be able to select the csv file to be read from their directory using an open file dialog box on a windows form.

So far I have been able to write some of the code so that the user can look for the file but I am not sure on how to link the file the user has chosen to the steamreader.

This is the code to read and write the csv file

try 
{
    using (StreamWriter sw = new StreamWriter("X:/PublishedSoftware/Data/NEWPYAEGON1PENSION.csv"))
    {
        using (StreamReader sr = new StreamReader(""))
        {

This is the code for the open file dialog box

private void btnFindAegonFile_Click(object sender, EventArgs e)
{
    openFileDialog1.Filter = "csv files(*.csv)|*.csv|All files(*.*)|*.*";
    openFileDialog1.FileName = "Browse for the AEGON file.";

    DialogResult result = openFileDialog1.ShowDialog();

    txtFindAegonFile.Text = this.openFileDialog1.FileName;

Solution

  • I got it working I used:

    string readfilename = txtFindAegonFile.Text;
    
    try
    {
        using (StreamReader sr = new StreamReader(readfilename))
        using (StreamWriter sw = new StreamWriter("X:/PublishedSoftware/Data/NEWPYAEGON1PENSION.csv"))
    }