Reputation:
I am trying to get my file stream to read whatever text file the user selects. The file path will be in a text box after they select it.
I want to use this file path so that my streamreader knows what file to read.
"Stream fileStream = FilePath.Text;" is not working.
public void ValidateButton_Click(object sender, EventArgs e)
{
{
List<string> temp = new List<string>();
string[] finalArray;
Stream fileStream = FilePath.Text;
using (StreamReader reader = new StreamReader(fileStream))
{
// We read the file then we split it.
string lines = reader.ReadToEnd();
string[] splittedArray = lines.Split(',');
// We will check here if any of the strings is empty (or just whitespace).
foreach (string currentString in splittedArray)
{
if (currentString.Trim() != "")
{
// If the string is not empty then we add to our temporary list.
temp.Add(currentString);
}
}
// We have our splitted strings in temp List.
// If you need an array instead of List, you can use ToArray().
finalArray = temp.ToArray();
}
}
I get the error can't convert string to system.io.
How can I get the streamreader to read the chosen file from the "FilePath" text box
Upvotes: 0
Views: 1596
Reputation: 467
FilePath.Text
returns a string which is the location of the file on the drive
the below code would work
using (StreamReader reader = new StreamReader(FilePath.Text))
{
// We read the file then we split it.
string lines = reader.ReadToEnd();
string[] splittedArray = lines.Split(',');
// We will check here if any of the strings is empty (or just whitespace).
foreach (string currentString in splittedArray)
{
if (currentString.Trim() != "")
{
// If the string is not empty then we add to our temporary list.
temp.Add(currentString);
}
}
// We have our splitted strings in temp List.
// If you need an array instead of List, you can use ToArray().
finalArray = temp.ToArray();
}
Upvotes: 1