c# open text file code example
Example 1: c sharp how to read a text file
string text = System.IO.File.ReadAllText(@"C:\filepath\file.txt");
string[] lines = System.IO.File.ReadAllLines(@"C:\filepath\file.txt");
Example 2: c# open text file
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
using (FileStream fs = File.Create(path))
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file.");
fs.Write(info, 0, info.Length);
}
}
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
Example 3: how to read a text file C#
string[] text = System.IO.File.ReadAllLines(@"C:\users\username\filepath\file.txt");