C#使用Dictionary<string, string>拆分字符串与记录log方法

这篇文章介绍了Dictionarystring,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方法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。

本文标题为:C#使用Dictionary<string, string>拆分字符串与记录log方法

基础教程推荐