File.Write() issue

Essentially, the code below is meant for reading and writing to a single line in a text file. I keep getting an error. I am having difficulty getting the following to work:

 //Read a specific line of text public static string ReadaLine(string path, int lineNum) { //string line = File.ReadLines(path).Skip(lineNum - 1).Take(1).First(); string line = File.ReadLines(path).Skip(lineNum).Take(1).First(); //Console.WriteLine(line + " read line"); return line; } //Append text to a specific line public static void AppendLine(string path, string text, int lineNum) { string linetext = ReadaLine(path, lineNum); OpenAdd(path, linetext, lineNum); } public static void OpenAdd(string path, string tobeadded) { // Open the stream and write to it. using (StreamWriter sw = File.AppendText(path)) { sw.WriteLine(tobeadded); } } public static void OpenAdd(string path, string tobeadded, int line) { // Open the stream and write to it. using (FileStream fs = File.OpenWrite(path)) { /* Byte[] info = new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method."); */ AddText(fs, tobeadded, line); // Add some information to the file. //fs.Write(tobeadded, 0, info.Length); } } public static void AddText(FileStream fs, string value, int line) { byte[] info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, line, info.Length); } 

by redlikeazebra via /r/csharp

Leave a Reply