c# wince 文本操作

来源:互联网 发布:在淘宝买到假货怎么办 编辑:程序博客网 时间:2024/05/22 06:22
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();            }        }    }

网上转的,不错
原创粉丝点击