How to read/write files in .Net Core?
What are the options to read/write files in .Net Core?
I am working on my first .Net Core app and looking for
File.Read*
/File.Write*
functions (System.IO
from .Net
) alternatives.
Package: System.IO.FileSystem
System.IO.File.ReadAllText("MyTextFile.txt"); ?
FileStream fileStream = new FileStream("file.txt", FileMode.Open);
using (StreamReader reader = new StreamReader(fileStream))
{
string line = reader.ReadLine();
}
Using the System.IO.FileStream and System.IO.StreamReader. You can use System.IO.BinaryReader or System.IO.BinaryWriter as well.
To write:
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(System.IO.File.Create(filePath).Dispose()))
{
file.WriteLine("your text here");
}
Works in Net Core 2.1
var file = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "email", "EmailRegister.htm");
string SendData = System.IO.File.ReadAllText(file);
Use:
File.ReadAllLines("My textfile.txt");
Reference: https://msdn.microsoft.com/pt-br/library/s2tte0y1(v=vs.110).aspx