• 常见辅助类、方法


    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 "注册成功";
                }
    
            }
    
        }
    Log.cs

    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;
                }
            }
    Encode-Decode

    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("
    ", ";");//读取的字符串(去掉换行符)
                //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();
                }
            }
    getConfig

    未完待续...

  • 相关阅读:
    LeetCode 3-Longest Substring Without Repeating Characters
    LeetCode 2-Add Two Numbers
    LeeCode 1-Two Sum
    Python中Lambda, filter, reduce and map 的区别
    解决Eclipse10配置Pydev不成功的问题
    Pythonxy下载链接
    546C. Soldier and Cards
    欧拉工程第58题:Spiral primes
    欧拉工程第57题:Square root convergents
    Linux编辑器vim键盘详解
  • 原文地址:https://www.cnblogs.com/tiancaige/p/12720030.html
Copyright © 2020-2023  润新知