这篇文章介绍了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方法


基础教程推荐
猜你喜欢
- Unity shader实现多光源漫反射以及阴影 2023-03-04
- 使用c#从分隔文本文件中插入SQL Server表中的批量数据 2023-11-24
- c#读取XML多级子节点 2022-11-05
- C# – NetUseAdd来自Windows Server 2008和IIS7上的NetApi32.dll 2023-09-20
- 京东联盟C#接口测试示例分享 2022-12-02
- C#中类与接口的区别讲解 2023-06-04
- C# Winform实现石头剪刀布游戏 2023-01-11
- C#通过GET/POST方式发送Http请求 2023-04-28
- c#中利用Tu Share获取股票交易信息 2023-03-03
- C#集合查询Linq在项目中使用详解 2023-06-09