解决方案 »

  1.   

    这里面有个示例代码。http://msdn.microsoft.com/zh-cn/library/System.IO.StreamWriter(v=vs.110).aspx
      

  2.   

    读取txt文本文件的所有数据
    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
                    openFileDialog1.ShowDialog();
                    textBox1.Text = openFileDialog1.FileName;
                    StreamReader SReader = new StreamReader(textBox1.Text, Encoding.Default);
                    textBox2.Text = SReader.ReadToEnd();
                }
                catch { MessageBox.Show("请选择文件"); }
            }
      

  3.   

    写文件,代码来自:
    How to: Write Text to a File
    http://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.110).aspxusing System;
    using System.IO;class Test
    {
        public static void Main()
        {
            try
            {
                using (StreamReader sr = new StreamReader("TestFile.txt"))
                {
                    String line = sr.ReadToEnd();
                    Console.WriteLine(line);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
      

  4.   

    上面是读文件,链接也错了,看这个吧How to: Read Text from a File
    http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx
    using System;
    using System.IO;class Test
    {
        public static void Main()
        {
            try
            {
                using (StreamReader sr = new StreamReader("TestFile.txt"))
                {
                    String line = sr.ReadToEnd();
                    Console.WriteLine(line);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }How to: Write Text to a File
    http://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.110).aspx
    using System;
    using System.IO;
    using System.Text;
    using System.Collections.Generic;class Program
    {
        static void Main(string[] args)
        {
            // Set a variable to the My Documents path. 
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            StringBuilder sb = new StringBuilder();        // Enumerate the files in the My Documents path, filtering for text files only. 
            foreach (string txtName in Directory.EnumerateFiles(mydocpath,"*.txt"))
            {
                // Open a stream reader and write the name of the file, a visual separator,  
                // and the contents of the file to the stream. 
                using (StreamReader sr = new StreamReader(txtName))
                {
                    sb.AppendLine(txtName.ToString());
                    sb.AppendLine("= = = = = =");
                    sb.Append(sr.ReadToEnd());
                    sb.AppendLine();
                    sb.AppendLine();
                }
            }
            // Write the stream contents to a new file named "AllTxtFiles.txt".
            using (StreamWriter outfile = new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
            {
                outfile.Write(sb.ToString());
            }
        }
    }