C#使用Dictionary<string, string>拆分字符串与记录log方法
Dictionary<string, string>拆分字符串
private Dictionary<string, string> GenDictionary(byte[] inMsg) { Dictionary<string, string> dictionary = new Dictionary<string, string>(); string arg= Encoding.Default.GetString(inMsg); char[] trimChars = new char[1]; string text = arg.TrimEnd(trimChars); string[] array = text.Split(new char[] { ';' }); for (int i = 0; i < array.Length; i++) { string text2 = array[i]; if (!string.IsNullOrEmpty(text2) && !dictionary.ContainsKey(text2.Split(new char[] { ':' })[0])) { dictionary.Add(text2.Split(new char[] { ':' })[0], text2.Replace(text2.Split(new char[] { ':' })[0] + ":", "")); } } return dictionary; }
记录log的方法
public static void WriteLog(string strLog) { string pathName = Environment.CurrentDirectory + "\\LOG\\" + DateTime.Now.ToString("yyyyMMdd"); string FileName = "Execute.log"; FileName = pathName + "\\" + FileName; if (!Directory.Exists(pathName)) { Directory.CreateDirectory(pathName); } FileStream fileStream = null; StreamWriter streamWriter = null; try { if (File.Exists(FileName)) { //追加记录内容 fileStream = new FileStream(FileName, FileMode.Append, FileAccess.Write); } else { //新建文件并记录 fileStream = new FileStream(FileName, FileMode.Create, FileAccess.Write); } streamWriter = new StreamWriter(fileStream); streamWriter.WriteLine("【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】" + strLog); } finally { streamWriter.Close(); fileStream.Close(); } }
到此这篇关于Dictionary<string, string>拆分字符串与记录log方法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)