2

How can I read multiple csv file in a folder? I have a program that map a csv file into its correct format using csvhelper library. And here is my code:


static void Main()
    {
        var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.csv");<br>
        var tempFilePath = Path.GetTempFileName();

        using (var reader = new StreamReader(filePath))
        using (var csvReader = new CsvReader(reader))
        using (var writer = new StreamWriter(tempFilePath))
        using (var csvWriter = new CsvWriter(writer))
        {
            csvReader.Configuration.RegisterClassMap<TestMapOld>();
            csvWriter.Configuration.RegisterClassMap<TestMapNew>();
            csvReader.Configuration.Delimiter = ",";

            var records = csvReader.GetRecords<Test>();

            csvReader.Configuration.PrepareHeaderForMatch = header =>
            {
                var newHeader = Regex.Replace(header, @"\s", string.Empty);
                newHeader = newHeader.Trim();
                newHeader = newHeader.ToLower();

                return newHeader;
            };
            csvWriter.WriteRecords(records);
        }

        File.Delete(filePath);
        File.Move(tempFilePath, filePath);
    }
0

1 Answer 1

1

Since this is homework (and/or you are seemingly new to coding), I'll give you a very suitable answer that will give you many hours of fun and excitement as you go through the documentation and samples provided

You need

GetFiles(String, String)

Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

searchPattern

String The search string to match against the names of files in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

foreach, in (C# reference)

The foreach statement executes a statement or a block of statements for each element in an instance of the type that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface.

I'll leave the details up to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.