- Home /
String array from text file - C#
Hi everyone,
Can someone give me a simple example code that loads every line from text file into array separately? I spent quite a few hours searching for answers, but nothing.
Thanks!
a few hours?
i typed your subject line into google and got this as the first result...
I tried this. I'm getting an error:
FileNotFoundException: Could not find file "D:\temp\wordlist.txt". System.IO.FileStream..ctor (System.String path, File$$anonymous$$ode mode, FileAccess access, >FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at >/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO /FileStream.cs:305)
and i'm sure that filepath is correct
Well, your computer is convinced the path is not correct... Personally, I'm inclined to believe your computer on this one :p
Could you copy the line on which this error occurs (including the path you specify there)? Perhaps you made an error somewhere there...
To be sure btw. What platform are you testing this on, windows? + Build target (standalone, web, etc.)
Answer by Toon_Werawat · Apr 19, 2016 at 02:52 PM
Here is what I got for you from my text project.
Here is get path
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
Load file
FileStream fs = new FileSteam(path, FileMode.Open);
string content = "";
using (StreamReader read = new StreamReader(fs, true))
{
content = read.ReadToEnd();
}
Replace all newline part
string[] result = content.Regex.Split(content, "\r\n?|\n", RegexOptions.Singleline);
@Toon_Werawat ,"content.Regex.Split" gives me an error : 'string' does not contain a definition for 'Regex'.
Answer by drudiverse · Nov 18, 2018 at 02:16 AM
works fine, system IO
var textFilePath : String = Application.dataPath + "/TEXT/" + "hb.txt";
var arrLine : String[] = File.ReadAllLines(textFilePath);
print (arrLine[3]);
Your answer
Follow this Question
Related Questions
Splitting text from lines into variables 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Array Element in Foreach loop 1 Answer