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);
  • 相关阅读:
    web版仿微信聊天界面|h5仿微信电脑端案例开发
    h5仿微信聊天(高仿版)、微信聊天表情|对话框|编辑器
    原生wcPop.js消息提示框(移动端)、内含仿微信弹窗效果
    旅行app(游记、攻略、私人定制) | 顺便游旅行H5移动端实例
    【h5+c3】web前端实战项目、快装webapp手机案例源码
    h5区块链项目实战
    css3波纹特效、H5实现动态波浪
    H5移动端项目案例、web手机微商城实战开发
    HTML5仿微信聊天界面、微信朋友圈实例
    Android 给服务器发送网络请求
  • 原文地址:https://www.cnblogs.com/gzoof/p/10177746.html
Copyright © 2011-2022 走看看