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 + "\r\n";
                            }
                            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)\r\n^3;.*$", "\r");
File.WriteAllText(Server.MapPath("~/test.txt"), r);

 

posted on 2018-12-26 10:19  AlexGui  阅读(443)  评论(0编辑  收藏  举报

导航