c#怎么把textbox中的内容保存到txt

1
2
3
4
5
string  path =   "D:\\log.txt" ;
             using  (StreamWriter sw =  new  StreamWriter(path,  true ))
             {
                 sw.Write( "xxxxxxxxxx" );
             }

1
2
3
4
5
6
7
8
9
10
  if  ( this .folderBrowserDialog1.ShowDialog() == DialogResult.OK)
             {
                 string  path =  this .folderBrowserDialog1.SelectedPath +  @"\xxx.txt" ;
 
                 using  (StreamWriter writer =  new  StreamWriter(path))
                 {
                     writer.Write( this .textBox1.Text);
                 }
                 MessageBox.Show( "写入成功" );
             }

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为你提供一个简单的文本编辑器的窗体程序,并且附上代码和截图。 首先,你需要在 Visual Studio 创建一个 Windows 窗体应用程序。然后,在窗体添加一个菜单栏和一个文本框控件,用于实现文本编辑的功能。 接下来,你可以在菜单栏添加一些菜单项,例如“文件”、“编辑”、“格式”等。在这些菜单项,你可以添加一些子菜单项,例如“新建”、“打开”、“保存”、“另存为”、“剪切”、“复制”、“粘贴”、“撤销”等。 代码如下: ```csharp public partial class Form1 : Form { string filePath = ""; bool isSaved = false; public Form1() { InitializeComponent(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { if (isSaved) { textBox1.Clear(); filePath = ""; isSaved = false; } else { DialogResult result = MessageBox.Show("是否保存当前文本?", "提示", MessageBoxButtons.YesNoCancel); if (result == DialogResult.Yes) { saveToolStripMenuItem_Click(sender, e); textBox1.Clear(); filePath = ""; isSaved = false; } else if (result == DialogResult.No) { textBox1.Clear(); filePath = ""; isSaved = false; } } } private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "文本文件|*.txt"; if (openFileDialog.ShowDialog() == DialogResult.OK) { filePath = openFileDialog.FileName; textBox1.Text = File.ReadAllText(filePath); isSaved = true; } } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { if (filePath == "") { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "文本文件|*.txt"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { filePath = saveFileDialog.FileName; } else { return; } } File.WriteAllText(filePath, textBox1.Text); isSaved = true; } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "文本文件|*.txt"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { filePath = saveFileDialog.FileName; File.WriteAllText(filePath, textBox1.Text); isSaved = true; } } private void cutToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut(); } private void copyToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Copy(); } private void pasteToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Paste(); } private void undoToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo(); } private void fontToolStripMenuItem_Click(object sender, EventArgs e) { FontDialog fontDialog = new FontDialog(); if (fontDialog.ShowDialog() == DialogResult.OK) { textBox1.Font = fontDialog.Font; } } private void colorToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog colorDialog = new ColorDialog(); if (colorDialog.ShowDialog() == DialogResult.OK) { textBox1.ForeColor = colorDialog.Color; } } } ``` 最后,你需要在窗体添加一些事件处理程序,用于处理菜单项的点击事件,以及窗体的关闭事件。在点击菜单项时,你可以调用相应的方法来实现对文本框的操作。在窗体关闭事件,你可以判断用户是否保存了当前文本,如果没有保存,则弹出提示框提示用户是否保存。 截图如下: ![text-editor](https://img-blog.csdn.net/20180502215215447?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJvZ3JhbW1lcnRpbmdfc2VydmljZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值