How to read all CSV in a folder

c#, system.io.fileinfo

Solution

For you streamreader and writer

StreamReader sr = new StreamReader(@"C:\CSV\test.csv");
StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv");

You can place this within a method:

private void ConvertToIBANAccount(string fileName,string outFileName)
{
    using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
        {
            StreamReader sr = new StreamReader(fileName);
            StreamWriter sw = new StreamWriter(outFileName);
            Console.WriteLine("Bezig met converteren....");
            while (sr.Peek() >= 0) 
            {
            string line = sr.ReadLine(); 
                try
                {
                    string[] rowsArray = line.Split(';');
                    line = string.Empty;
                    string account = rowsArray[0];
                    string relationID = rowsArray[1];
                    string resultIBAN = client.BBANtoIBAN(account);
                    string resultBIC = client.BBANtoBIC(account);
                    if (resultIBAN != string.Empty && resultBIC != string.Empty)
                    {
                        line = account + ";" + resultIBAN +";" + resultBIC + ";" + relationID;
                    }

                    else
                    {
                        line = account + ";" + "0" + ";" + "0" + ";" + relationID;
                    }
                }
                catch (Exception msg)
                {
                    Console.WriteLine(msg);
                }
            sw.WriteLine(line) ;

            }
            sr.Close();
            sw.Close();
        }
        Console.WriteLine("Conversie succesvol");
        Console.ReadLine();
    }
}

Then find each file of extension .csv

string[] files = Directory.GetFiles(yourDirectoryName, "*.csv");
files.ToList().ForEach(f => ConvertToIBANAccount(f,"temp" + f));

Problem

I have a little piece of code in which I can read a CSV file and then my code converts the accounts in the CSV file to IBAN accounts. Somehow this gives me an error: ``` using (var client = new WebService.BANBICSoapClient("IBANBICSoap")) { StreamReader sr = new StreamReader(@"C:\CSV\test.csv"); StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv"); while (sr.Peek() >= 0) { string line = sr.ReadLine(); try { string[] rowsArray = line.Split(';'); line = string.Empty; string account = rowsArray[0]; string rel = rowsArray[1]; string res = acc; string rest = acct; if (resultIBAN != string.Empty && resultBIC != string.Empty) { line = account + ";" + res +";" + rest + ";" + rel; } else { line = account + ";" + "0" + ";" + "0" + ";" + rel; } } catch (Exception msg) { Console.WriteLine(msg); } sw.WriteLine(line) ; } sr.Close(); sw.Close(); } } ```

Original source