• VS使用openFileDialog读写txt和bin文件(C#语言)


    1、BIN文件

    (1) 创建布局文件并添加openFileDialog和saveFileDialog

    (2)打开按钮事件

    1. FileStream Myfile;
    2. BinaryReader binreader;
    3. BinaryWriter binwriter;
    4. int file_len;//bin文件长度
    5. int count = 1;
    6. byte[] binchar = new byte[] { };//文件数据存储
    7. private void button1_Click(object sender, EventArgs e)//打开文件按钮
    8. {
    9. openFileDialog1.Filter = "*.bin|*.BIN";
    10. if (openFileDialog1.ShowDialog() == DialogResult.OK)
    11. {
    12. fileName.Text = openFileDialog1.FileName;
    13. StringBuilder str = new StringBuilder();
    14. Myfile = new FileStream(openFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
    15. binreader = new BinaryReader(Myfile);
    16. binwriter = new BinaryWriter(Myfile);
    17. file_len = (int)Myfile.Length;//获取bin文件长度
    18. binchar = binreader.ReadBytes(file_len);
    19. foreach (byte j in binchar)
    20. {
    21. str.Append(j.ToString("X2") + " ");
    22. if (count >= 8)
    23. {
    24. count = 0;
    25. str.Append("\r\n");
    26. }
    27. count++;
    28. }
    29. text2.Text = str.ToString();
    30. MessageBox.Show("查询成功!");
    31. }
    32. }

    (3)写入按钮事件

    1. private Byte[] AsciiCharToByte8(string Str)
    2. {
    3. Byte[] tByte = Encoding.ASCII.GetBytes(Str);
    4. return tByte;
    5. }
    6. private void button2_Click(object sender, EventArgs e)//写入文件按钮
    7. {
    8. byte[] insert_data = AsciiCharToByte8("hello");//在打开的文件前加入hello
    9. byte[] binchar0 = new byte[file_len + 5];
    10. for (int i = 0; (i < insert_data.Length); i++)
    11. {
    12. binchar0[i] = insert_data[i];
    13. }
    14. for(int i=0;i
    15. {
    16. binchar0[i + 5] = binchar[i];
    17. }
    18. Myfile.Seek(0, SeekOrigin.Begin);
    19. binwriter.Write(binchar0);
    20. binreader.Close();
    21. binwriter.Close();
    22. Myfile.Close();
    23. }

    2、TXT文件

    (1)创建布局界面

    (2)打开按钮事件

    1. private void button_open_txt_Click(object sender, EventArgs e)
    2. {
    3. openFileDialog1.Filter = "(*.txt)|*.txt";
    4. if (openFileDialog1.ShowDialog() == DialogResult.OK)
    5. {
    6. fileName_txt.Text = openFileDialog1.FileName;
    7. textBox_display_txt.Clear();
    8. StreamReader sr = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default);
    9. string line;
    10. while ((line = sr.ReadLine()) != null)
    11. {
    12. textBox_display_txt.Text += line;
    13. }
    14. sr.Close();
    15. }
    16. }

    (3)写入按钮事件

    1. private void button_write_txt_Click(object sender, EventArgs e)
    2. {
    3. bool flag = true;
    4. StreamWriter sw = null;
    5. try
    6. {
    7. sw = new StreamWriter(openFileDialog1.FileName);//创建StreamWriter对象
    8. sw.WriteLine(textBox_display_txt.Text);
    9. }
    10. catch (Exception ex)
    11. {
    12. Console.WriteLine("写入流异常:" + ex.ToString());
    13. flag = false;
    14. }
    15. finally
    16. {
    17. sw.Close();//确保最后总会关闭流
    18. Console.WriteLine("写入流关闭");
    19. }
    20. if (flag)
    21. {
    22. MessageBox.Show("文件已保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    23. }
    24. }

  • 相关阅读:
    I/O设备的概念和分类,I/O控制器
    完美十进制数——去年天梯校赛
    Android Studio run main()方法报错
    FPGA内部资源
    全彩夜视+智能告警,夜间户外安防有TA就够了!
    windows无法完成格式化怎么办?
    【Day 3】Ajax + Vue 项目、路由 + Nginx
    大厂面试:如何用快排思想在O(n)内查找第K大元素?
    1000万条数据分页方法,redis提高访问速度,减少数据库压力
    前后端分离项目,vue+uni-app+php+mysql在线小说电子书阅读系统设计与实现(H5移动项目)
  • 原文地址:https://blog.csdn.net/m0_61973119/article/details/140924969