C# 正则替换、Json格式化等

工作需要做一个小工具,捡起之前的C# Winform

1、正则替换

 string strText="xxxxx";
 Regex reg = new Regex(@"\n- \d+ - ");
 strText = reg.Replace(strText, "");

2、Json格式化

public static string ConvertJsonString(string str)
        {
            //格式化json字符串
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ‘ ‘
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }

3、自定义ListBoxItem

        [Serializable]
        public class MyListBoxItem
        {
            public override string ToString()
            {
                if (IsGroup) return "" + ItemText + "";
                else return "       " + ItemText;
                return ItemText;
            }
            public bool IsGroup { get; set; }
            public string ItemText { get; set; }
        }

4、用到的Mqtt组件
M2MqttDotnetCore
5、序列化,反序列化

       private List<MyListBoxItem> LoadTopicList(string fileName)
        {
            try
            {
                string strParent = System.IO.Directory.GetParent(System.Windows.Forms.Application.ExecutablePath).FullName;
                string strFilePath = strParent + "\\config\\" + fileName + ".data";
                if (!File.Exists(strFilePath)) return new List<MyListBoxItem>();
                //反序列化
                FileStream fs = new FileStream(strFilePath, FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                List<MyListBoxItem> p = bf.Deserialize(fs) as List<MyListBoxItem>;
                fs.Close();
                return p;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            return new List<MyListBoxItem>();
        }
        private void SaveTopicList(string fileName, List<MyListBoxItem> list)
        {
            try
            {
                string strParent = System.IO.Directory.GetParent(System.Windows.Forms.Application.ExecutablePath).FullName;
                //序列化
                FileStream fs = new FileStream(strParent + "\\config\\" + fileName + ".data", FileMode.OpenOrCreate);

                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, list);
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

C# 正则替换、Json格式化等

上一篇:windows docker从阿里云私有仓库拉取镜像


下一篇:微信小程序之事件绑定