Easiest way to read from and write to files

后端 未结 12 1841
[愿得一人]
[愿得一人] 2020-11-22 08:04

There are a lot of different ways to read and write files (text files, not binary) in C#.

I just need something that is easy and uses the least amount of c

12条回答
  •  醉梦人生
    2020-11-22 08:24

    It's good when reading to use the OpenFileDialog control to browse to any file you want to read. Find the code below:

    Don't forget to add the following using statement to read files: using System.IO;

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
             textBox1.Text = File.ReadAllText(openFileDialog1.FileName);  
        }
    }
    

    To write files you can use the method File.WriteAllText.

提交回复
热议问题