[C#] Read from a file and write in a button?

Hello everyone,
I’m trying to make a script that checks if the file exists, then reads the first line of the file and then write the string found in the file on the button.
Any ideas?

If you’re on standalone, there’s a lot of answers on IO, see this.

If the file is in your resources folder:

var textAsset = Resources.Load("file path and name") as TextAsset;

textAsset.text will get you back the text, you could then split the lines and do whatever you want:

var lines = textAsset.text.Split('

');

lines[0] is the first line.

If the file is somewhere else in your hard drive (don’t know why would you wanna do that)

var sr = new StreamReader(File.Open("path", FileMode.Open));

You could then do sr.ReadToEnd() or sr.ReadLine() - all return a string.