• 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. }

  • 相关阅读:
    Python21天学习挑战赛Day(12-13)·requests模块全解
    unable to boot the simulator. domain nsposixerrordomain code 4
    java基于Springboot+vue的实验室设备申请预约管理系统 elelmentui 前后端分离
    2022.7台式机装机指南(3060 + 12490F)
    递归算法深入解析
    RCD负载箱的优势和特点与其他负载箱有何区别?
    Ubuntu安装mysql8遇到的问题以及详细安装过程
    Golang Web框架fiber
    一篇五分生信临床模型预测文章代码复现——Figure 2. 生存分析,箱线图表达改变分析(三)
    在Ubuntu上通过Portainer部署微服务项目
  • 原文地址:https://blog.csdn.net/m0_61973119/article/details/140924969