将文本框的内容保存到文件

发布于 2024-08-10 13:38:42 字数 44 浏览 4 评论 0原文

我正在开发一个具有文本框的应用程序。我想将其内容写入文件,但我该怎么做呢?

I'm developing an application that has a TextBox. I want to write its contents to a file, but how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情场扛把子 2024-08-17 13:38:42

有很多方法可以实现此目的,最简单的是:

 using(var stream = File.CreateText(path))
 {
      stream.Write(text);
 }

请务必查看 MSDN 页面中的 File.CreateTextStreamWriter.Write

如果您的目标不是 .NET Compact Framework,正如您的标签所示,您可以做得更简单:

 File.WriteAllText(path, string);

There are many ways to accomplish this, the simplest being:

 using(var stream = File.CreateText(path))
 {
      stream.Write(text);
 }

Be sure to look at the MSDN page for File.CreateText and StreamWriter.Write.

If you weren't targeting the .NET Compact Framework, as your tags suggest, you could do even simpler:

 File.WriteAllText(path, string);
天荒地未老 2024-08-17 13:38:42
System.IO.File.WriteAllText("myfile.txt", textBox.Text);

如果您被 BCL 的某些脑死亡版本所困扰,那么您可以自己编写该函数:

static void WriteAllText(string path, string txt) {
    var bytes = Encoding.UTF8.GetBytes(txt);
    using (var f = File.OpenWrite(path)) {
        f.Write(bytes, 0, bytes.Length);
    }
}
System.IO.File.WriteAllText("myfile.txt", textBox.Text);

If you're stuck on some brain-dead version of the BCL, then you can write that function yourself:

static void WriteAllText(string path, string txt) {
    var bytes = Encoding.UTF8.GetBytes(txt);
    using (var f = File.OpenWrite(path)) {
        f.Write(bytes, 0, bytes.Length);
    }
}
尝蛊 2024-08-17 13:38:42

试试这个:

using System.Text;
using System.IO;
static void Main(string[] args)
{
  // replace string with your file path and name file.
  using (StreamWriter sw = new StreamWriter("line.txt"))
  {
    sw.WriteLine(MyTextBox.Text);
  }
}

当然,添加异常处理等。

Try this:

using System.Text;
using System.IO;
static void Main(string[] args)
{
  // replace string with your file path and name file.
  using (StreamWriter sw = new StreamWriter("line.txt"))
  {
    sw.WriteLine(MyTextBox.Text);
  }
}

Of course, add exception handling etc.

伤感在游骋 2024-08-17 13:38:42

对于 richTextBox,您可以为此添加一个“保存”按钮。还从工具箱添加一个 saveFileDialog 控件,然后在按钮的单击事件中添加以下代码。

private void button1_Click(object sender, EventArgs e)
{
    DialogResult Result = saveFileDialog1.ShowDialog();//Show the dialog to save the file.
    //Test result and determine whether the user selected a file name from the saveFileDialog.
   if ((Result == DialogResult.OK) && (saveFileDialog1.FileName.Length > 0))
   {
       //Save the contents of the richTextBox into the file.
       richTextBox1.SaveFile(saveFileDialog1.FileName);
   } 
}

For a richTextBox, you can add a "Save" button for this purpose. Also add a saveFileDialog control from Toolbox, then add following code in button's click event.

private void button1_Click(object sender, EventArgs e)
{
    DialogResult Result = saveFileDialog1.ShowDialog();//Show the dialog to save the file.
    //Test result and determine whether the user selected a file name from the saveFileDialog.
   if ((Result == DialogResult.OK) && (saveFileDialog1.FileName.Length > 0))
   {
       //Save the contents of the richTextBox into the file.
       richTextBox1.SaveFile(saveFileDialog1.FileName);
   } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文