0

I am trying to allow the user to specify the path for the file that is used to generate an ArrayList, which is then used for a Chart, using the text inputted via a TextBox (the string is in the format of @"C:\filename.csv" which was previously used and worked when the path was hard coded). However, I get an unhandled argument exception error each time the code is executed. How do I go about fixing this error?

    private ArrayList readData()
    {
        logFile = textFilePath.Text;

        // initialise an array list for storing the data
        ArrayList logData = new ArrayList();

        // read every line in the file
        using (StreamReader reader = new StreamReader(logFile))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                // split the line into a string array using the comma 
                // delimiter and add to the array list
                string[] parts = line.Split(',');
                logData.Add(parts);
            }
        }

        // strip the first element from our array list, as this contains
        // the header information from our file
        logData.RemoveAt(0);
        // return the array list containing the file contents
        return logData;

    }
CuriousLekgolo
  • 107
  • 1
  • 1
  • 8
  • There are tools like CSVHelper which will parse CSVs in one line of code and put the result in something more current than an ArrayList. Not likely you will get much help until you describe where the exception happens – Ňɏssa Pøngjǣrdenlarp Jan 02 '17 at 19:02
  • Ah I see. My bad. "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll" is the precise error and it happens when beginning the StreamReader process (the line beginning "using"). – CuriousLekgolo Jan 02 '17 at 19:07
  • Might want to add that to the actual question. It *sounds like* the textbox doesnt have a valid and/or complete file path. What is it doing in a text box? Rather than burden the user with typing the full name use a FileOpenDialog, then in that method posted, you can use `FileReadAllLines` to read in teh file and process the array returned line by like. Parsing CSVs using String.Split() can be very problematic; hence the plethora of tools to do it right – Ňɏssa Pøngjǣrdenlarp Jan 02 '17 at 19:11
  • May help: [Reading CSV file in with some missing columns](http://stackoverflow.com/a/41168048/1070452) The question is VB, but the answer doesnt use any VB specific methods. It should show you how easy something like CSVHelper is – Ňɏssa Pøngjǣrdenlarp Jan 02 '17 at 19:16
  • Ah I see. I shall look into FileOpenDialog. It would appear that I should have done more research before asking this question so poorly. I apologise but thank you for pointing me into the right direction. – CuriousLekgolo Jan 02 '17 at 19:22

0 Answers0