C# 文件读取

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(!String.IsNullOrEmpty(txtName.Text) && !String.IsNullOrEmpty(txtPassword.Text))
            {
                if(txtName.Text == "itcast" && txtPassword.Text == "123")
                {
                    MessageBox.Show("登录成功!");
                }
                else
                {
                    MessageBox.Show("用户名或者密码有误!");
                    txtPassword.Text = "";
                }
            }
            else
            {
                MessageBox.Show("用户名或者密码不能为空!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPassword.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.*)|*.*";
            ofd.RestoreDirectory = true;//记忆上次浏览路径
            if(ofd.ShowDialog()==DialogResult.OK)
            {
                DirectoryInfo dir = Directory.GetParent(ofd.FileName); //获取文件所在的目录
                textBox1.Text = dir.ToString() + "\\";
            }
           // string path = ofd.FileName;
            //textBox1.Text = File.ReadAllText(path);
        }

        private void buttonTransform_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            TransformFiles(textBox1.Text.Trim());
        }

        public void TransformFiles(string path)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                DirectoryInfo[] dirs = dir.GetDirectories();    //获取子目录
                FileInfo[] files = dir.GetFiles("*.*");
                foreach(DirectoryInfo d in dirs)
                {
                    TransformFiles(dir + d.ToString() + "\\");
                }
                foreach(FileInfo f in files)
                {
                    listBox1.Items.Add(dir + f.ToString());
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        //打开保存 - 打开按钮
        private void buttonOpen_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.txt)|*.txt|(*.*)|*.*";
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = ofd.FileName;
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                try
                {
                    ofd.OpenFile();
                    string line = sr.ReadLine();//读取文本行
                    while (line != null)
                    {
                        textBox3.Text += line + "\n";
                        line = sr.ReadLine();
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                finally
                {
                    sr.Close();
                    fs.Close();
                }
            }
        }

        //打开保存 - 保存按钮
        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "(*.txt)|*.txt|(*.*)|*.*";
            sfd.AddExtension = true;
            sfd.RestoreDirectory = true;
            if(sfd.ShowDialog()==DialogResult.OK)
            {
                textBox2.Text = sfd.FileName;
                FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                try
                {
                    sw.Write(textBox3.Text);
                    sw.Flush();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                finally
                {
                    sw.Close();
                    fs.Close();
                }
            }
        }

        //读写文本 - 写入数据按钮
        private void buttonWrite_Click(object sender, EventArgs e)
        {
            if (!(Directory.Exists(@"E:\temp")))
            {
                Directory.CreateDirectory(@"E:\temp");
            }
            string filePath = @"E:\temp\qq.doc";
            if (File.Exists(filePath))
            {
                labelResult.ForeColor = Color.Red;
                labelResult.Text = "当前文件已经存在!";
                return;
            }
            FileStream fs = new FileStream(filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            try
            {
                sw.Write(textBox4.Text);
                sw.Flush();
                labelResult.ForeColor = Color.Green;
                labelResult.Text = "写入数据完成!";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }

        //读写文本 - 读取数据按钮
        private void buttonRead_Click(object sender, EventArgs e)
        {
            textBox5.Text = "";
            string filePath = @"E:\temp\qq.doc";
            if (!File.Exists(filePath))
            {
                labelResult.ForeColor = Color.Red;
                labelResult.Text = filePath + "文件不存在!";
                return;
            }
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            try
            {
                string line = sr.ReadLine();
                while (line != null)
                {
                    textBox5.Text += line + "\n";
                    line = sr.ReadLine();
                }
                labelResult.ForeColor = Color.Green;
                labelResult.Text = "读取数据成功!";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                sr.Close();
                fs.Close();
            }
        }

        private void buttonWrite2_Click(object sender, EventArgs e)
        {
            string filePath = @"E:\temp\test.data";
            if(!(Directory.Exists(@"E:\temp")))
            {
                Directory.CreateDirectory(@"E:\temp");
            }
            if(File.Exists(filePath))
            {
                labelResult2.ForeColor = Color.Red;
                labelResult2.Text = "文件已经存在!";
                return;
            }
            FileStream fs = new FileStream(filePath, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            for(int i = 0; i < 200; i++)
            {
                bw.Write((int)i);
            }
            bw.Flush();
            labelResult2.ForeColor = Color.Green;
            labelResult2.Text = "写入数据成功!";
            bw.Close();
            fs.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox6.Text = "";
            string filePath = @"E:\temp\test.data";
            if (!(File.Exists(filePath)))
            {
                labelResult2.ForeColor = Color.Red;
                labelResult2.Text = filePath + "文件不存在!";
                return;
            }
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            for(int i=0;i<200;i++)
            {
                Int32 intTmp = br.ReadInt32();
                textBox6.Text += "->" + intTmp.ToString();
            }
            labelResult2.ForeColor = Color.Green;
            labelResult2.Text = "读取数据成功!";
            br.Close();
            fs.Close();
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值