我想把一个Text Box 中的内容 写进 一个 文本文档中  其中 包括 换行符  
 请问应该怎么做   呢

解决方案 »

  1.   

    StreamWriter writer=new StreamWriter(fileName);
    writer.Write(TextBox1.Text);
    writer.Flush();
    writer.Close()
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    namespace TestWriteDownTextBoxContext
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.TextBox textBoxObject;
    private System.Windows.Forms.Button buttonObject; public Form1()
    {
    InitializeComponent(); textBoxObject = new System.Windows.Forms.TextBox();
    textBoxObject.Text = "";
    textBoxObject.Multiline = true;
    textBoxObject.Anchor = AnchorStyles.Left|AnchorStyles.Right|AnchorStyles.Top|AnchorStyles.Bottom;
    textBoxObject.Location = new System.Drawing.Point(10,10);
    textBoxObject.Size = new System.Drawing.Size(200,135); buttonObject = new System.Windows.Forms.Button();
    buttonObject.Text = "&Save";
    buttonObject.Location = new System.Drawing.Point(230,135);
    buttonObject.Size = new System.Drawing.Size(60,20); this.ClientSize = new System.Drawing.Size(296,162);
    this.Controls.Add(this.textBoxObject);
    this.Controls.Add(this.buttonObject);
    this.buttonObject.Click += new System.EventHandler(this.buttonObject_Click);
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    } private void buttonObject_Click(object sender, System.EventArgs e)
    {
    StreamWriter textBoxStreamWriter = new StreamWriter("C:\\test.txt",false);
    textBoxStreamWriter.Write(textBoxObject.Text);
    textBoxStreamWriter.Close();
    MessageBox.Show("File save to C:\\test.txt");
    }
    }
    }
      

  3.   

    关键要添加using System.IO和StreamWriter textBoxStreamWriter = new StreamWriter("C:\\test.txt",false);textBoxStreamWriter.Write(textBoxObject.Text);
    textBoxStreamWriter.Close();