Skip to content

Instantly share code, notes, and snippets.

@Mahno74
Last active July 6, 2021 10:38
Show Gist options
  • Save Mahno74/270b5132de8f2ff898feb0b6787e0d98 to your computer and use it in GitHub Desktop.
Save Mahno74/270b5132de8f2ff898feb0b6787e0d98 to your computer and use it in GitHub Desktop.
Запись в текстовый файл данных
using System;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
string readPath = @"D:\read.txt"; //путь для чтения файла
string writePath = @"D:\write.txt"; //пусть для записи файла
string text = ""; //буферная переменная
//Чтение
try
{
using (StreamReader sr = new StreamReader(readPath, Encoding.Default)) //Чтение в переменную
{
text = sr.ReadToEnd();
}
using (StreamWriter sw = new StreamWriter(writePath, false, Encoding.Default)) //false - переписываем файл
{
sw.WriteLine(text);
}
using (StreamWriter sw = new StreamWriter(writePath, true, Encoding.Default)) //true - дозаписываем файл
{
sw.WriteLine("Bonus ");
sw.Write(4.5);
}
}
catch (Exception)
{
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment