标签:i++ 设置 substr oar ide sed put class 日志记录
1.日志记录Log.cs
public class Log { private string Event { get; set; } private DateTime Time { get; set; } /// <summary> /// 日志记录,写入文件 /// </summary> /// <param name="Eventname">事件名</param> /// <param name="Time">时间</param> public void RegisterLog(string Eventname, string Time) { //判断是否已经有了这个文件 if (!System.IO.File.Exists("log.txt")) { //没有则创建这个文件 FileStream fs1 = new FileStream("log.txt", FileMode.Create, FileAccess.Write);//创建写入文件 System.IO.File.SetAttributes(@"log.txt", FileAttributes.Hidden); //设置文件属性为隐藏 StreamWriter sw = new StreamWriter(fs1); sw.WriteLine(Eventname.Trim() + " " + Time.Trim());//开始写入值 sw.Close(); fs1.Close(); //return "注册成功"; } else { //有则追加字符串 FileStream fs = new FileStream("log.txt", FileMode.Append, FileAccess.Write); System.IO.File.SetAttributes(@"log.txt", FileAttributes.Hidden); StreamWriter sr = new StreamWriter(fs); sr.WriteLine(Eventname.Trim() + " " + Time.Trim());//开始写入值 sr.Close(); fs.Close(); // return "注册成功"; } } }
2.字符串加密解密
/// <summary> /// 字符串加密 /// </summary> /// <param name="encryptString"></param> /// <returns></returns> public static string Encode(string encryptString) { try { string KEY = "zjp1202!"; byte[] _vector = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; var rgbKey = Encoding.UTF8.GetBytes(KEY.Substring(0, 8)); var des = new DESCryptoServiceProvider(); var inputByteArray = Encoding.UTF8.GetBytes(encryptString); var ms = new MemoryStream(); var cs = new CryptoStream(ms, des.CreateEncryptor(rgbKey, _vector), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (Exception ex) { return null; } } /// <summary> ///字符串解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public static string Decode(string decryptString) { try { string KEY = "zjp1202!"; byte[] _vector = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; var provider = new DESCryptoServiceProvider(); var rgbKey = Encoding.UTF8.GetBytes(KEY.Substring(0, 8)); var inputByteArray = Convert.FromBase64String(decryptString); var ms = new MemoryStream(); var cs = new CryptoStream(ms, provider.CreateDecryptor(rgbKey, _vector), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); var encoding = new UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch (Exception ex) { return null; } }
3.读取txt文件
public static string getConfig(string filename) { //读取网络上的txt文件 //WebClient client = new WebClient(); //byte[] buffer = client.DownloadData("http://域名/myset.txt"); // FileStream file = new FileStream("\\config.txt", FileMode.Open); //byte[] buffer = byte[] buffer = ReadFile(filename); string res = System.Text.ASCIIEncoding.ASCII.GetString(buffer); string str = res.Replace("\r\n", ";");//读取的字符串(去掉换行符) //string[] items = str.Split(‘;‘);//放进数组里 //for(int i=0;i<items.Length;i++) //{ //} return str; } //读filename到byte[] public static byte[] ReadFile(string fileName) { FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate); byte[] buffer = new byte[fs.Length]; try { fs.Read(buffer, 0, buffer.Length); fs.Seek(0, SeekOrigin.Begin); return buffer; } catch { return buffer; } finally { if (fs != null) fs.Close(); } }
未完待续...
标签:i++ 设置 substr oar ide sed put class 日志记录
原文地址:https://www.cnblogs.com/tiancaige/p/12720030.html