Setting A String To A .txt's File Contents?
I want to set a string to the entire contents of a file without manually (with my own code) looping through the fi开发者_如何转开发le and adding each line to the string. Is there something built in that will accomplish this?
string asdf = file("aFile.txt");
Looks like you want File.ReadAllText
. If you don't pass an encoding in, it will use UTF-8... but make sure that's actually what you want, or specify an encoding yourself.
One of these:
String FileStuff = File.ReadAllText("MyFile.txt");
String[] FileLines = File.ReadAllLines("MyFile.txt");
精彩评论