Reputation: 2518
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.
Upvotes: 84
Views: 166702
Reputation: 91
Works in Net Core 2.1
var file = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "email", "EmailRegister.htm");
string SendData = System.IO.File.ReadAllText(file);
Upvotes: 9
Reputation: 1607
For writing any Text to a file.
public static void WriteToFile(string DirectoryPath,string FileName,string Text)
{
//Check Whether directory exist or not if not then create it
if(!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
string FilePath = DirectoryPath + "\\" + FileName;
//Check Whether file exist or not if not then create it new else append on same file
if (!File.Exists(FilePath))
{
File.WriteAllText(FilePath, Text);
}
else
{
Text = $"{Environment.NewLine}{Text}";
File.AppendAllText(FilePath, Text);
}
}
For reading a Text from file
public static string ReadFromFile(string DirectoryPath,string FileName)
{
if (Directory.Exists(DirectoryPath))
{
string FilePath = DirectoryPath + "\\" + FileName;
if (File.Exists(FilePath))
{
return File.ReadAllText(FilePath);
}
}
return "";
}
For Reference here this is the official microsoft document link.
Upvotes: 2
Reputation: 11
public static void Copy(String SourceFile, String TargetFile)
{
FileStream fis = null;
FileStream fos = null;
try
{
Console.Write("## Try No. " + a + " : (Write from " + SourceFile + " to " + TargetFile + ")\n");
fis = new FileStream(SourceFile, FileMode.Open, FileAccess.ReadWrite);
fos = new FileStream(TargetFile, FileMode.Create, FileAccess.ReadWrite);
int intbuffer = 5242880;
byte[] b = new byte[intbuffer];
int i;
while ((i = fis.Read(b, 0, intbuffer)) > 0)
{
fos.Write(b, 0, i);
}
Console.Write("Writing file : " + TargetFile + " is successful.\n");
break;
}
catch (Exception e)
{
Console.Write("Writing file : " + TargetFile + " is unsuccessful.\n");
Console.Write(e);
}
finally
{
if (fis != null)
{
fis.Close();
}
if (fos != null)
{
fos.Close();
}
}
}
The code above will read a big file and write to a new big file. The "intbuffer" value can be set in multiple of 1024. While both source and target file are open, it reads the big file by bytes and write to the new target file by bytes. It will not go out of memory.
Upvotes: 1
Reputation: 115
To write:
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(System.IO.File.Create(filePath).Dispose()))
{
file.WriteLine("your text here");
}
Upvotes: 8
Reputation: 97
Use:
File.ReadAllLines("My textfile.txt");
Reference: https://msdn.microsoft.com/pt-br/library/s2tte0y1(v=vs.110).aspx
Upvotes: 6
Reputation: 1866
Package: System.IO.FileSystem
System.IO.File.ReadAllText("MyTextFile.txt"); ?
Upvotes: 104
Reputation: 910
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.
Upvotes: 36