更新时间:2022-03-28 23:05:57
没有功能你正在寻找什么(AppendAllText附加到你已经看到的文件的末尾)...
所以你必须自己做一些事情...
1.使用 ReadAllLines方法 [读入所有行。 a href =https://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]
2.在你得到的字符串数组上运行并添加'OK'st每个人敲响
3.使用 WriteAllLines方法 [ ^ ]
There is no function that does what you are looking for (AppendAllText appends to the end of the file as you already saw)...
So you have to do something by yourself...
1. Read-in all the lines using ReadAllLines method[^]
2. Run on the string array you got and add the 'OK' string to each
3. Write-out all the lines using WriteAllLines method[^]
尝试如下(.net 3.5或更高版本)
try as below (.net 3.5 or later)
File.WriteAllLines(filePath, File.ReadAllLines(filePath).Select(line=>line+"OK"));
.net 3.0
.net 3.0
string textToAppend = "ok";
List<string> lines = new List<string>();
foreach (var line in File.ReadAllLines(filePath))
{
lines.Add(line + textToAppend);
}
File.WriteAllLines(filePath, lines.ToArray());
this is entire code, i want to move file (in button btnSave) and if file exist to overwrite that file, how to do that?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace ToEditTxt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// sakrivanje dugmeta -obradi- i dugmeta -snimi- i promena boje
btnEdit.Enabled = false;
btnEdit.BackColor = Color.White;
btnSave.Enabled = false;
btnSave.BackColor = Color.White;
}
private void btnLoad_Click(object sender, EventArgs e)
{
//menjanje boje dugmeta i sakrivanje dugmeta
btnLoad.Enabled = false;
btnLoad.BackColor = Color.White;
// omogućavanje dugmeta -obradi- i promena boje
btnEdit.Enabled = true;
btnEdit.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
//učitavanje fajla
openFileDialog1.ShowDialog();
label1.Text = openFileDialog1.FileName;
label2.Text = System.IO.Path.GetFileName(openFileDialog1.FileName);
label3.Text = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
label4.Visible = true;
}
private void btnEdit_Click(object sender, EventArgs e)
{
//menjanje boje dugmeta i sakrivanje dugmeta
btnEdit.Enabled = false;
btnEdit.BackColor = Color.White;
//dodavanje 1 na kraju svakog reda u texualnom fajlu
string textToAppend = "1";
List<string> lines = new List<string>();
foreach (var line in File.ReadAllLines(label1.Text))
{
lines.Add(line + textToAppend);
}
File.WriteAllLines(label1.Text, lines.ToArray());
// omogućavanje dugmeta -snimi- i promena boje
btnSave.Enabled = true;
btnSave.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
//snimanje i preacivanje fajla u drugi direktorijum
System.IO.FileInfo f = new System.IO.FileInfo(label1.Text);
f.MoveTo(@"c:\q\" + label3.Text + ".rpt");
if (File.Exists(@"c:\q\"))
{
File.Delete(@"c:\q\");
File.Move(label3.Text + ".rpt", @"c:\q\");
}
File.Move(label3.Text + ".rpt", @"c:\q\");
}
catch (IOException)
{
//resetovanje labela na početne vrednosti
label1.ResetText();
label2.ResetText();
label3.ResetText();
this.Refresh();
this.ResetText();
this.Update();
// vraćanje grafičkog okruženja na početne vrednosti
btnLoad.Enabled = true;
btnEdit.Enabled = true;
btnLoad.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
btnEdit.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
label5.Visible = true;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
</string></string>