C#创建Windows窗体应用程序实例12【文件管理】

都实例11了?你还不会创建项目?不会操作步骤?不会写代码?请进入下方链接学习吧!

C#创建Windows窗体应用程序实例1

C#创建Windows窗体应用程序实例2

1. 知识点

打开文件并读出文本  方法1

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

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

        rd1.Close();

打开文件并读出文本  方法2

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

                string line;        

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

                        Console.WriteLine(line); 

                        } 

        } 

打开文件并写入文本   方法1

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

        sw.WriteLine(“Suzhou”);

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

打开文件并写入文本   方法2

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

                sw.WriteLine(“Suzhou”);        

                sw.WriteLine(“125.3”);

        }

创建FileStream对象(打开文件)

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

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

文件定位

        文件开始 fs.Seek(0,SeekOrigin.Begin);

        当前位置 fs.Seek(0,SeekOrigin.Current);

        文件结尾 fs.Seek(0,SeekOrigin.End);

写入/读取数据

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

        fs.Write(info, 0, info.Length); //写入字节数据    

        byte[] b=new byte[1024];

        fs.Read(b, 0, b.Length); //读取字节数据

关闭流

        fs.Close();  

        或采用 using 语句

2. 样式设计

在工具中选择 以下 控件

 

 

 

 

3. 代码实现 

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. 执行效果  

猜你喜欢

转载自blog.csdn.net/qq_45037155/article/details/124316080
今日推荐