- Home /
Read a specific line from .txt file.
The .txt contains the Name, Bank and Dmg of a specific character. I don't know how to get the 2nd line (Name) and place it as the name string.
void GrabInfoFromSheet()
{
Recog = "Player" + PlayerDataNr.ToString();
StreamReader reader = new StreamReader(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments).ToString() + "/CombatBot/" + Recog);
Name = reader.ReadLine();
}
Answer by AevinGames · Feb 25, 2017 at 03:35 PM
I found what I was meant to do by following the link @jmgek gave me.
Recog = "Player" + PlayerDataNr + ".txt";
string[] lines = System.IO.File.ReadAllLines(DefaultDataPath+ "/IGPlayers/" + Recog);
Name = lines[0];
Answer by IMemeManI · Jan 22, 2018 at 04:44 PM
Anyone wanting to pull a random line from the string list, here's the code I assembled from Aevin's answer:
string path = "Assets/Resources/names.txt";
string[] lines = System.IO.File.ReadAllLines (path);
c.name = lines[Random.Range(0,lines.Length)];
In my case I used it to pull a random name from a .txt which has over 100 names on different lines. Hope this might help someone looking for this kind of answer!
Answer by jmgek · Feb 24, 2017 at 10:01 PM
I would suggest using serialized data or JSon in your case, because it sounds like you would benefit from a data object.
But if you still want to use a text file just use:
//Edit
var strContent;
//Edit
using (var reader = new StreamReader(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments).ToString() + "/CombatBot/" + Recog))
{
strContent = reader.ReadToEnd();
}
List<string> lines = new List<string>(strContent);
//print second line
print(lines[1]);
https://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx
I'm getting the error: Argument 1: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable' @jmgek
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Reading from a .txt file until a certain character is detected 1 Answer
Xml Serialization? (how to read/write?) 1 Answer
Photon Unity - Only receiving one value. 3 Answers