当前位置 : 主页 > 网络编程 > c#编程 >

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

来源:互联网 收集:自由互联 发布时间:2023-01-31
Dictionarystring, string拆分字符串 private Dictionarystring, string GenDictionary(byte[] inMsg){Dictionarystring, string dictionary = new Dictionarystring, string();string arg= Encoding.Default.GetString(inMsg);char[] trimChars = new c

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#线程开发之System.Thread类详解
下一篇:没有了
网友评论