C# Create Windows Forms Application Instance 12 [File Management]

All instance 11? Don't you create a project yet? Don't know the steps? Can't write code? Please visit the link below to learn!

C# Create Windows Forms Application Instance 1

C# Create Windows Forms Application Instance 2

1. Knowledge points

Open the file and read the text Method 1

        StreamReader  rd1=new StreamReader(@”c:\a.txt”);  

        while((line=rd1.ReadLine())!=null)  { Console.WriteLine(line); }      

        rd1.Close();

Open the file and read the text Method 2

        using(StreamReader  d1=new StreamReader(@”c:\a.txt”)) {    

                string line;        

                while((line=rd1.ReadLine())!=null) { 

                        Console.WriteLine(line); 

                        } 

        } 

Open file and write text Method 1

        StreamWriter  sw=new StreamWriter(@”c:\a.txt”);

        sw.WriteLine(“Suzhou”);

        sw.WriteLine(“125.3”); sw.Close();

Open file and write text Method 2

        using(StreamWriter  sw=new StreamWriter(@”c:\a.txt”)) {        

                sw.WriteLine(“Suzhou”);        

                sw.WriteLine(“125.3”);

        }

Create FileStream object (open file)

        方法1 FileStream  fs=new FileStream(@”c:\a.txt”,FileMode.Create);

        Method 2 FileStream fs = File.Create (@ ”c: \ a.txt”);

file location

        File start fs.Seek(0,SeekOrigin.Begin);

        current position fs.Seek(0,SeekOrigin.Current);

        End of file fs.Seek(0,SeekOrigin.End);

write/read data

        byte[] info=new UTF8Encoding(true).GetBytes(“A”);

        fs.Write(info, 0, info.Length); //Write byte data    

        byte[] b=new byte[1024];

        fs.Read(b, 0, b.Length); //Read byte data

close stream

        fs.Close();  

        or with a using statement

2. Style Design

Select the following  controls in the tool

 

 

 

 

3. Code implementation 

using System.Windows.Forms;
using System.IO;

namespace 显示和保存文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        //”显示文件”的Click事件的代码
        private void button1_Click(object sender, EventArgs e)
        {
            string file = textBox1.Text;
            if (!File.Exists(@file))
                MessageBox.Show("文件" + @file + "不存在");
            else
            {
                int a;
                FileStream fs = new FileStream(@file, FileMode.Open, FileAccess.Read);
                richTextBox1.Clear();
                a = fs.ReadByte();
                while (a != -1)       //是否读到文件末尾
                {
                    richTextBox1.Text += ((char)a).ToString();
                    a = fs.ReadByte();
                }
                fs.Close();
            }
        }
        //”保存文件”的Click事件的代码
        private void button2_Click(object sender, EventArgs e)
        {
            byte a;
            string file = textBox1.Text;
            FileStream fs = new FileStream(@file, FileMode.OpenOrCreate, FileAccess.Write);
            for (int i = 0; i < richTextBox1.Text.Length; i++)
            {
                a = (byte)richTextBox1.Text[i];
                fs.WriteByte(a);
            }
            fs.Flush();   //将写缓冲区的数据写入内存
            fs.Close();
        }
    }
}

4. Execution effect  

Guess you like

Origin blog.csdn.net/qq_45037155/article/details/124316080