Importing Data and assigning it to a variable

Hello, I am trying to import a set of data from a text file that is approximately and assign one column of this data to a variable. Has anyone done anything like this?

Look into fileIO a bit. It gives functions, like opening a file, reading one line/character, etc.

1 Like

check out System.IO.File.ReadAllLines(@“path\filename.ext”) this will read the data in and then using String.Split to convert each line to a string[ ] array. Then the string[0] should be first column. There are a lot of examples if u search bing or google.

Have fun

You should also look at deceloper rules, there’s one rule, that everytime, you want to post something, search it on google, if you still can’t solve the problem, post.

1 Like

heres a sample using List, not tested didnt have time sorry

public List FileToListOfString(string FileName,int colIndex)
{

List values = new List(); //creates a string list
string[ ] strLines = File.ReadAllLines(FileName); //strLines holds each line of the file

foreach (string strLine in strLines) //for each line in the loaded lines strLines get the column passed in via colIndex
{
//assuming its a comma seperated file else replace ‘,’ with what ever char your data is split by ie ‘;’ or ‘|’ etc
values.Add(strLine.Split(‘,’)[colIndex].ToString());
}

return values;
}

MagicZelda, you should use code tags, they are under import/code on the comment toolbar.

sorry was late and ive never put code up before lol. Will remember for next time