c# - 使用 Winforms 删除文本表单文本文件
问题描述
如何从 Winforms 中的文本文件中删除?
我的目标是从文本框中删除选定的文本,它也可以在我的文本文件中删除。
具体来说,我的需要是,如果用户从文本框中删除文本,它也会从文本文件中删除。
我的代码是:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(textBox1.Text);
textBox2.Text = sr.ReadToEnd();
sr.Close();
}
private void button3_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(textBox1.Text, true);
sw.WriteLine(textBox2.Text);
sw.Close();
}
private void button4_Click(object sender, EventArgs e)
{
textBox2.SelectedText = "";
string selectedText = "theTextYouWantToDelete";
string fileContent = File.ReadAllText(@"C:\demo\demo.txt");
File.WriteAllText(@"C:\demo\demo.txt",
fileContent.Replace(selectedText, ""));
}
private bool SelectedText(char arg)
{
throw new NotImplementedException();
}
}
解决方案
这将用selectedText
空字符串替换
string selectedText = textBox2.Text;
string fileContent = File.ReadAllText(@"C:\demo.txt");
File.WriteAllText(@"C:\demo.txt", fileContent.Replace(selectedText, ""));
推荐阅读
- javascript - jQuery手动调用点击功能
- tensorflow - 如何对具有编码图像字符串张量作为输入的张量流模型的输入进行编码
- java - 如何使用 javax.swing.text.AsyncBoxView 将 JTextPane 中的文本布局委托给非 EDT 线程?
- .net - 如何更改 DateTime AM PM 格式?
- android - 使用 Glide 使用 Storage References 中的图像填充 RecycleView
- javascript - 如何在选择输入中设置多个值?
- stm32 - STM32CubeIDE 不生成配置代码 (NUCLEO-H745ZI)
- dart - 在 Dart 中,有没有比大型 switch 语句更好的解决方案
- sql-server - 视图上最大(日期)查询的奇怪查询计划
- vb.net - Visual Basic 使用 FOR...NEXT 循环在多个标签中显示查询的所有输出