zoukankan      html  css  js  c++  java
  • C#文件处理相关内容

    C#修改文件内容和过滤有效内容,文件格式一定。

                OpenFileDialog file = new OpenFileDialog();//定义新的文件打开位置控件  
                file.InitialDirectory = Application.StartupPath;
                file.Filter = "mot file(*.mot)|*.mot|hex file(*.hex)|*.hex|s19 files(*.s19)|*.s19";
                file.FilterIndex = 0;
                file.RestoreDirectory = true;
                if (file.ShowDialog() == DialogResult.OK)
                {
                    if (file.FileName != null)
                    {
                        try
                        {
                            //FileStream fs = new FileStream(file.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                            //
                            StreamReader sr = new StreamReader(file.FileName);
                            string text = "";//读出来即将要写入的内容
                            string line = sr.ReadLine();
                            while (line != null)
                            {
                                if (line.Contains("S3"))
                                {
                                    string checkS = "";
                                    //1.更改地址
                                    line = line.Replace("S32508FB", "S32500FB");
                                    //2.去除原来的checkSum
                                    line = line.Substring(0, line.Length - 2);
                                    //3.计算checkSum
                                    checkS = checkSum(line);
                                    line = line + checkS;
                                    Console.WriteLine(line);
                                    text += line + "
    ";
                                }
                                else
                                {
                                }
                                line = sr.ReadLine();
                            }
                            sr.Close();
                            sr.Dispose();
    
                            //更改保存文本
                            StreamWriter sw = new StreamWriter(file.FileName, false);
                            sw.WriteLine(text);
                            sw.Close();
                            sw.Dispose();
                            //fs.Close();
                            //fs.Dispose();
    
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        strFilePath = file.FileName;
                    }
                    else
                    { return; }
                }

    checkSum的算法实现

     public string checkSum(string line)//计算文件一行的校验和(一行文件string不包含最后两位校验和)返回string为校验和结果一字节
            {
                string checksum = null;
                int sum = 0;
                string[] shu = new string[line.Length / 2];
                for (int i = 1; i < line.Length / 2; i++)
                {
                    shu[i] = line.Substring(i * 2, 2);
                    sum += Convert.ToInt32(shu[i], 16);
                }
                int jieguo = 255 - sum;
                string he = Convert.ToString(jieguo, 16).PadLeft(2, '0');
                checksum = he.Substring(he.Length - 2, 2).ToUpper();
                return checksum;
    
            }

    C#替换文本内容,数据量不太大的话直接Replace为""。

    using System.IO;
    
    //读取文本 
    StreamReader sr = new StreamReader(文本文件的路径);
    string str = sr.ReadToEnd();
    sr.Close();
    //替换文本
    str = str.Replace("E103","E103**");
    //更改保存文本
    StreamWriter sw = new StreamWriter(文本文件的路径,false);
    sw.WriteLine(str);
    sw.Close();

    C#删除文件内容,用正则表达式

    string s = File.ReadAllText(Server.MapPath("~/test.txt"));
    string r = Regex.Replace(s, @"(?m)
    ^3;.*$", "
    ");
    File.WriteAllText(Server.MapPath("~/test.txt"), r);
  • 相关阅读:
    Windows JScript 在 游览器 中运行 调试 Shell 文件系统
    autohotkey 符号链接 软连接 symbolink
    软链接 硬链接 测试
    SolidWorks 修改 基准面 标准坐标系
    手机 路径 WebDAV 映射 驱动器
    Win10上手机路径
    explorer 命令行
    单位公司 网络 封锁 屏蔽 深信 AC
    cobbler自动化部署原理篇
    Docker四种网络模式
  • 原文地址:https://www.cnblogs.com/gzoof/p/10177746.html
Copyright © 2011-2022 走看看