Open solomem opened 1 year ago
WriteAllLines()
and WriteAllText()
for example uses StreamWriter
behind the scene. Here is the reflector output:
public static void WriteAllLines(string path, string[] contents, Encoding encoding)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
using (StreamWriter writer = new StreamWriter(path, false, encoding))
{
foreach (string str in contents)
{
writer.WriteLine(str);
}
}
}
public static void WriteAllText(string path, string contents, Encoding encoding)
{
using (StreamWriter writer = new StreamWriter(path, false, encoding))
{
writer.Write(contents);
}
}
Generally I'd go with
System.IO.File
overStreamReader
as the former is mostly a convenient wrapper for the latter. consider the code behindFile.OpenText
:Or
File.ReadAllLines
:You can use Reflector to check out some other methods, as you can see it's pretty straightforward
For reading the contents of the file, take a look at:
File.ReadAllLines File.ReadAllText