Sprache: C#
Liest den Text in einem Textfile ein.
using System.IO
public string readTextFile(string strFilePath)
{
string strContent = string.Empty;
if (File.Exists(strFilePath))
{
StreamReader myFile = new StreamReader(strFilePath, Encoding.Default);
strContent = myFile.ReadToEnd();
myFile.Close();
}
return strContent;
}
public string readTextFile(string strFilePath)
{
string strContent = string.Empty;
if (File.Exists(strFilePath))
{
StreamReader myFile = new StreamReader(strFilePath, Encoding.Default);
strContent = myFile.ReadToEnd();
myFile.Close();
}
return strContent;
}
Alte URL:
/snippet/textfile-einlesen/974
Hallo,
geht mit noch kürzer:
[code]
string text = null;
if (File.Exists(path))
text = File.ReadAllText(path);
[/code]
1. Gehts kürzer (Siehe Kommentar von Günther Foidl) und 2. wird der Stream bei einem Fehler nicht geschlossen. (STichwort: using-Block)