这篇文章介绍了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方法
基础教程推荐
猜你喜欢
- C# List实现行转列的通用方案 2022-11-02
- ZooKeeper的安装及部署教程 2023-01-22
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# 调用WebService的方法 2023-03-09
- C# windows语音识别与朗读实例 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- winform把Office转成PDF文件 2023-06-14
- C#类和结构详解 2023-05-30
- unity实现动态排行榜 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22