Read a .csv file containing static parameters

27 views Asked by At

I have got a csv file, with equal coumns and unequal rows, I would like to call some static parameters from this CSV which method is the most appropriate?

class Script
{
    static void Main()
    {
       // Specify the path to the CSV file on the source server
       string sourceFilePath = @"\\source_server\path\to\parameters.csv";



        // Read parameters from the CSV file
        string[] parameters = ReadParametersFromCSV(sourceFilePath);



        // Print the parameters
        foreach (var parameter in parameters)
        {
            Console.WriteLine(parameter);
        }
    }



    static string[] ReadParametersFromCSV(string filePath)
    {
        try
        {
            // Read all lines from the CSV file
            string[] lines = File.ReadAllLines(filePath);



            // Assume the first line contains headers, and skip it
            // If you want to include headers, remove the Skip(1) part
            string[] parameters = lines.Skip(1).ToArray();



            return parameters;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error reading parameters: {ex.Message}");
            return Array.Empty<string>();
        }
    }
}}
0

There are 0 answers