打开现有的文件,追加一行
我想打开一个文本文件,向它追加一行,然后closures它。
您可以使用File.AppendAllText
:
File.AppendAllText(@"c:\path\file.txt", "text content" + Environment.NewLine);
using (StreamWriter w = File.AppendText("myFile.txt")) { w.WriteLine("hello"); }
select一个! 但是第一个很简单。 最后可能用于文件操作:
//Method 1 (I like this) File.AppendAllLines( "FileAppendAllLines.txt", new string[] { "line1", "line2", "line3" }); //Method 2 File.AppendAllText( "FileAppendAllText.txt", "line1" + Environment.NewLine + "line2" + Environment.NewLine + "line3" + Environment.NewLine); //Method 3 using (StreamWriter stream = File.AppendText("FileAppendText.txt")) { stream.WriteLine("line1"); stream.WriteLine("line2"); stream.WriteLine("line3"); } //Method 4 using (StreamWriter stream = new StreamWriter("StreamWriter.txt", true)) { stream.WriteLine("line1"); stream.WriteLine("line2"); stream.WriteLine("line3"); } //Method 5 using (StreamWriter stream = new FileInfo("FileInfo.txt").AppendText()) { stream.WriteLine("line1"); stream.WriteLine("line2"); stream.WriteLine("line3"); }
可能要检查TextWriter类。
//Open File TextWriter tw = new StreamWriter("file.txt"); //Write to file tw.WriteLine("test info"); //Close File tw.Close();
或者你可以使用File.AppendAllLines(string, IEnumerable<string>)
File.AppendAllLines(@"C:\Path\file.txt", new[] { "my text content" });
File.AppendText将做到这一点:
using (StreamWriter w = File.AppendText("textFile.txt")) { w.WriteLine ("-------HURRAY----------"); w.Flush(); }
//我们可以用
公共StreamWriter(stringpath,布尔附加);
同时打开文件
StreamWriter SW = new StreamWriter(Path,true);
第一个参数是保存文件完整path的string第二个参数是Append Mode,在这种情况下,它被设置为true string Path =“C:\ MyFolder \ Notes.txt”
写在文件上可以完成
SW.Write(串)
要么
SW..WriteLine(串)
SW.WriteLine(“Some Text”);
SW.Flush();
SW.Close();
示例代码
private void WriteAndAppend() { string Path = Application.StartupPath + "\\notes.txt"; FileInfo fi = new FileInfo(Path); StreamWriter SW; StreamReader SR; if (fi.Exists) { SR = new StreamReader(Path); string Line = ""; while (!SR.EndOfStream) // Till the last line { Line = SR.ReadLine(); } SR.Close(); int x = 0; if (Line.Trim().Length <= 0) { x = 0; } else { x = Convert.ToInt32(Line.Substring(0, Line.IndexOf('.'))); } x++; SW = new StreamWriter(Path, true); SW.WriteLine("-----"+string.Format("{0:dd-MMM-yyyy hh:mm:ss tt}", DateTime.Now)); SW.WriteLine(x.ToString() + "." + textBox1.Text); } else { SW = new StreamWriter(Path); SW.WriteLine("-----" + string.Format("{0:dd-MMM-yyyy hh:mm:ss tt}", DateTime.Now)); SW.WriteLine("1." + textBox1.Text); } SW.Flush(); SW.Close(); }
//display sample reg form in notepad.txt using (StreamWriter stream = new FileInfo("D:\\tt.txt").AppendText())//ur file location//.AppendText()) { stream.WriteLine("Name :" + textBox1.Text);//display textbox data in notepad stream.WriteLine("DOB : " + dateTimePicker1.Text);//display datepicker data in notepad stream.WriteLine("DEP:" + comboBox1.SelectedItem.ToString()); stream.WriteLine("EXM :" + listBox1.SelectedItem.ToString()); }
技术上最好的方法可能是这里:
private static async Task AppendLineToFile([NotNull] string path, string line) { if (string.IsNullOrWhiteSpace(path)) throw new ArgumentOutOfRangeException(nameof(path), path, "Was null or whitepsace."); if (!File.Exists(path)) throw new FileNotFoundException("File not found.", nameof(path)); using (var file = File.Open(path, FileMode.Append, FileAccess.Write)) using (var writer = new StreamWriter(file)) { await writer.WriteLineAsync(line); await writer.FlushAsync(); } }