0

Just trying out C# to make a button that loads csv files verify them and parse them:

protected void Upload_Btn_Click(object sender, EventArgs e)
{
    string test = PNLdataLoader.FileName;
    //checks if file is csv
    Regex regex = new Regex("*.csv");
    Match match = regex.Match(test);
    if (match.Success)
    {
        string CSVFileAsString = System.Text.Encoding.ASCII.GetString(PNLdataLoader.FileBytes);
        System.IO.MemoryStream MS = new System.IO.MemoryStream(PNLdataLoader.FileBytes);
        System.IO.StreamReader SR = new System.IO.StreamReader(MS);
        //Store each line in CSVlines array of strings
        string[] CSVLines = new string[0];
        while (!SR.EndOfStream)
        {
            System.Array.Resize(ref CSVLines, CSVLines.Length + 1);

            CSVLines[CSVLines.Length - 1] = SR.ReadLine();

        }
    }

So far I got it to store the lines in CSVLines but I am not sure what is wrong with the regex. Is there a more efficient way to do this?

2
  • I don't see a two dimensional array, did I miss something? Commented Apr 19, 2014 at 7:03
  • @MarkF I did not get to that part yet still looking at the documentation on how to do it. Commented Apr 19, 2014 at 8:34

1 Answer 1

0

That isn't a valid expression, its saying match whatever character that comes before * 0 or more times, since there is no character before that there is a problem.

This will probably match most things, it does not include special characters.

Regex regex = new Regex("[a-zA-Z0-9]{1,}.csv"); 

You could also do this instead:

if(test.EndsWith(".csv"))

and lastly, I would change your array to a List<T> or something like that, futher explained here: What is more efficient: List<T>.Add() or System.Array.Resize()?

//Store each line in CSVlines array of strings
List<string> CSVLines = new List<string>();
while (!SR.EndOfStream)
{
    CSVLines.Add(SR.ReadLine());

}

EDIT: List<T> is in System.Collections.Generic

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.