C# 普通的辅助类

在数字前面补0

        /// <summary>
        /// 在数字前面添加0
        /// </summary>
        /// <param name="num">数字</param>
        /// <param name="ws">添加0后的位数</param>
        /// <returns></returns>
        public string numPadLeftzero(string num ,int ws)
        {
            return num.ToString().PadLeft(ws, '0'); 
        }
在数字前面补0
        /// <summary>
        /// 去除数字前面的0 
        /// </summary>
        /// <param name="num">数字</param>
        /// <returns></returns>
        public string numRemovezero(string num)
        {
            string r = @"[1-9]\d+";
            string n = System.Text.RegularExpressions.Regex.Match(num, r).ToString();    //提取信息 
            return n;
        }
去除数据前面的0
/// <summary>
        /// 将传入的数据写入TXT文件中
        /// <param name="data">要储存的数据</param>
        public static void writeLogData(string data)
        {
            string path = Application.StartupPath + "\\Log\\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            path = path + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            if (!File.Exists(path))
            {
                FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                long fl = fs.Length;
                fs.Seek(fl, SeekOrigin.End);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "==>" + data);//开始写入值
                sw.Close();
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                long fl = fs.Length;
                fs.Seek(fl, SeekOrigin.Begin);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "==>" + data);//开始写入值
                sw.Close();
                fs.Close();
            }

        }
将数据保存到xml中

 

posted @ 2019-05-13 09:32  游林  阅读(286)  评论(0编辑  收藏  举报