c# wince 文本操作

23 篇文章 0 订阅
c# wince 文本操作
internal class TxtManager
    {
        private static string appPath =
            System.IO.Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());

        private static string dbSetTxtA = appPath + @"\DB_Set_A.ini"; //设置的文本文件
        private static string dbSetTxtB = appPath + @"\DB_Set_B.ini"; //用来临时存储A中的内容

        public string[,] readDBSetTxtA() //读取文本文件 存储到数组中
        {
            StreamReader sr;
            FileStream fs = new FileStream(dbSetTxtA, FileMode.Open, FileAccess.Read);
            sr = new StreamReader(fs);
            string strLine = sr.ReadLine();
            string[,] result = new string[10, 20];
            int i = 0;
            while (strLine != null) //
            {
                //string _serial = "";
                //string _sign = "";
                //string _data = "";
                //11 11 11 11 11 01 04 250 200 50 12 40 2.836226 16 50 0 1000 822.4379 1 20
                //0  1  2  3  4  5  6  7   8   9  10 11 12       13 14 15 16  17       18 19
                string[] arrTxt = strLine.Split(' ');
                for (int j = 0; j < arrTxt.Length; j++)
                {
                    result[i, j] = arrTxt[j];
                }
                strLine = sr.ReadLine();
                i++;
            }
            return result;
        }

 

        public void writeTxt(byte[] serial, byte[] sign, float[] data) //写文本文件 如果关键值 在数组文本中不存在 则将数组的内容写到文本中
        {
            string _serial = "";
            string _sign = "";
            string _data = "";
            string _stringAll = "";
            string _serial_sign = "";

            for (int i = 0; i < serial.Length; i++)
            {
                _serial = _serial + " " + serial[i].ToString("X2");
            }
            for (int i = 0; i < sign.Length; i++)
            {
                _sign = _sign + " " + sign[i].ToString("X2");
            }

            for (int i = 0; i < data.Length; i++)
            {
                _data = _data + " " + data[i].ToString();
            }

            _serial_sign = _serial.Trim() + " " + _sign.Trim();
            _stringAll = _serial_sign + " " + _data.Trim();
            StreamWriter sw;
            StreamReader sr;
            if (File.Exists(dbSetTxtA)) //文本文件存在
            {
                //该处目的 将以前设置内容备份,
                //新写入A中内容,之后读取以前设置的内容,如果已经含有现写入A中的设备的设置,则B中的内容不导入到A中
                //保证A中每个设备的设置都是最新的
                File.Copy(dbSetTxtA, dbSetTxtB, true);
                sw = new StreamWriter(dbSetTxtA, false); //程序路径下不存在文本文件,则 创建文本文件
                sw.WriteLine(_stringAll.Trim()); //将设置内容写到dbSetTxtA

                FileStream fs = new FileStream(dbSetTxtB, FileMode.Open, FileAccess.Read);

                sr = new StreamReader(fs);
                string strLine = sr.ReadLine();

                while (strLine != null) //
                {
                    //   MessageBox.Show(strLine.Substring(0, _serial_sign.Length) + "\n" + _serial_sign);

                    //如果B中的“区域值+" "+设备值”在A刚写入的内容中存在 说明该内容被更新
                    if (strLine.Substring(0, _serial_sign.Length) == _serial_sign)
                    {
                        //存在则不添加
                    }
                    else
                    {
                        sw.WriteLine(strLine);
                    }
                    strLine = sr.ReadLine();
                }
                sr.Close();
                sw.Close();
                File.Delete(dbSetTxtB);
            }
            else
            {
                sw = new StreamWriter(dbSetTxtA); //程序路径下不存在文本文件,则 创建文本文件
                sw.WriteLine(_stringAll.Trim());
                sw.Close();
            }
        }
    }

网上转的,不错
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值