Как добавить в файл нужное количество байт? - C#
Формулировка задачи:
Приветствую вас. Вопрос в том как мне в середине файла добавить массив байт?
Решение задачи: «Как добавить в файл нужное количество байт?»
textual
Листинг программы
- using System.IO;
- using System.Text;
- namespace ConsoleApplication26
- {
- class Program
- {
- static void Insert(string fileName, long position, byte[] newData)
- {
- FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
- fs.Seek(position, SeekOrigin.Begin);
- byte[] secondPart = new byte[fs.Length - position];
- fs.Read(secondPart, 0, secondPart.Length);
- fs.Seek(position, SeekOrigin.Begin);
- fs.Write(newData, 0, newData.Length);
- fs.Write(secondPart, 0, secondPart.Length);
- fs.Close();
- }
- static void Main(string[] args)
- {
- File.WriteAllText("test.txt", "12345678");
- Insert("test.txt", 3, Encoding.UTF8.GetBytes("abc"));
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д