一、配置文件读写类
用于在开发时候C#操作配置文件读写信息
- 1、工具类 ReadIni 代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace TcpServer
{public class ReadIni{[DllImport("kernel32")]// 读配置文件方法的6个参数:所在的分区(section)、 键值、 初始缺省值、 StringBuilder、 参数长度上限 、配置文件路径public static extern long GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder retVal, int size, string filePath);[DllImport("kernel32")]//写入配置文件方法的4个参数: 所在的分区(section)、 键值、 参数值、 配置文件路径private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);public static string FileNmae = "SysConfig.ini";/*读配置文件*/public static string GetValue(string section, string key){ string fileName = Directory.GetCurrentDirectory() + "/" + ReadIni.FileNmae;if (File.Exists(fileName)) //检查是否有配置文件,并且配置文件内是否有相关数据。{StringBuilder sb = new StringBuilder(255); GetPrivateProfileString(section, key, "配置文件不存在,读取未成功!", sb, 255, fileName);return sb.ToString();}else{return string.Empty;}}/*写配置文件*/public static void SetValue(string section, string key, string value){ string fileName = Directory.GetCurrentDirectory() + "/" + ReadIni.FileNmae;WritePrivateProfileString(section, key, value, fileName); // 路径会自动创建}}
}
- 2、使用方法
// 自定义配置文件名称
ReadIni.FileNmae ="Config.ini"; // 默认SysConfig.ini
// 设置配置文件内容ReadIni.SetValue("配置", "测试","王小宝好");// 读取配置文件内容string value = ReadIni.GetValue("配置","测试");
3、效果
二、日志记录类
在项目开发中我们经常要对业务进行日志记录,方便出现问题后对于故障的排查。这里我们使用C#实现了简单的日志记录功能。
- 1、日志记录类代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace TcpServer
{public class Logger{private string logPath;private string DirPath;public Logger(string path){DirPath = path;}public Logger(){DirPath = Directory.GetCurrentDirectory()+"/logs/"+DateTime.Now.ToString("yyyyMMdd"); }public void LogInfo(string message){if (!Directory.Exists(DirPath)) { Directory.CreateDirectory(DirPath); }logPath = DirPath + "/log-info.log";Log("INFO", message);}public void LogWarning(string message){if (!Directory.Exists(DirPath)) { Directory.CreateDirectory(DirPath); }logPath = DirPath + "/log-warning.log";Log("WARNING", message);}public void LogError(string message){if (!Directory.Exists(DirPath)) { Directory.CreateDirectory(DirPath); }logPath =DirPath+ "/log-error.log";Log("ERROR", message);}private void Log(string level, string message){string logEntry = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} - {level} - {message}{Environment.NewLine}";File.AppendAllText(logPath, logEntry);}}
}
- 2、使用方法
Logger Log = new Logger();
Log.LogInfo("记录一条日志信息");
Log.LogWarning("记录警告日志");
Log.LogError("记录错误日志");
- 3、记录效果
三、数据缓存类
数据缓存类是一个用C#实现的对数据进行缓存的简单功能
- 1、数据缓存类实现代码
using System;
using System.Collections.Generic;namespace TcpServer
{public class CacheHelper<TKey, TValue>{private readonly Dictionary<TKey, CachedItem> _cache = new Dictionary<TKey, CachedItem>(); public CacheHelper(){}public void Set(TKey key, TValue value,int tTime= 3600){_cache[key] = new CachedItem { ExpTime= TimeSpan.FromSeconds(tTime), Created = DateTime.UtcNow, Value = value };}public bool TryGet(TKey key, out TValue value){CachedItem cachedItem;if (_cache.TryGetValue(key, out cachedItem) && cachedItem.IsValid){value = (TValue)cachedItem.Value;return true;}value = default(TValue);return false;}public bool Remove(TKey key){return _cache.Remove(key);}public void Clear(){_cache.Clear();}// 辅助类,用于跟踪缓存项的创建时间和有效期private class CachedItem{public TimeSpan ExpTime { get; set; }public DateTime Created { get; set; }public object Value { get; set; }public bool IsValid{get{return DateTime.UtcNow - Created < ExpTime;}}}}
}
- 2、使用方法
CacheHelper<string, object> cache = new CacheHelper<string, object>(); // 设置缓存 缓存20秒 cache.Set("key1", "value1",20);// 读取缓存object value = string.Empty;if (cache.TryGet("key1", out value)){ return value;}
- 3、使用效果需要用心体会