The problem is the path you're passing to the
parseCSV
method:
parseCSV(System.Convert.ToString(myStream))
Convert.ToString
will call the
ToString
method on the object you passed in. Since
Stream
doesn't override that method, this will call the
ToString
method defined on the
object
class, which simply returns the type name -
System.IO.FileStream
.
You then pass that string to the
StreamReader
constructor, which is expecting a file path. Since it's not a fully qualified file path, the string you've passed in will be combined with the current working directory -
C:\Users\collmark\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Release\ - to create the full path of the file to open.
Since there isn't a file in the path
C:\Users\collmark\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Release\System.IO.FileStream, you then get a
FileNotFoundException
.
You should pass the
Stream
to the
parseCSV
method, and pass that on to the
StreamReader
constructor which accepts a
Stream
argument.
You'll also need to change how the
parseCSV
method works. With the current code, it would return a list of
string
values, one per row of the file, where each string would be
"System.String[]"
. Instead, you need to return a list of string arrays, where each array represents a single row, and the elements of the array represent the values.
Something like this:
public List<string[]> parseCSV(Stream fileStream)
{
using (StreamReader readFile = new StreamReader(fileStream))
{
List<string[]> parsedData = new List<string[]>();
string line;
string[] row;
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(',');
parsedData.Add(row);
}
return row;
}
}
private void loadData()
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "HellsingRPG");
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
using (Stream myStream = openFileDialog1.OpenFile())
{
if (myStream != null)
{
List<string[]> myData = parseCSV(myStream);
if (myData.Count != 0)
{
string[] firstRow = myData[0];
if (firstRow.Length >= 29)
{
textBox1.Text = myData[0];
...
numericUpDown22.Value = Convert.ToDecimal(myData[28]);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.ToString());
}
}
}
NB: Environment.GetEnvironmentVariable(@"%MyDocuments%\HellsingRPG\")
will always return
null
, as there is no environment variable with that name. Either use the
ExpandEnvironmentVariables
method, or a combination of the
Environment.GetFolderPath
and
Path.Combine
methods.
NB 2: The
Convert.ToDecimal
lines will fail if the value read from the file isn't a valid number. You should consider using
the decimal.TryParse
method[
^] instead.
NB 3: Whilst this simple parser should work with the file that you've created, real CSV files can be much more complicated. For example, field values can contain commas, quotes, and new-lines. There are lots of ready-made libraries to handle this for you - for example,
CsvHelper[
^].