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'); }
/// <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; }
/// <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(); } }