浏览代码

字符串,URL,文本文件工具完善

yuzhengyang 1 年之前
父节点
当前提交
05885ba5f1

+ 1 - 0
Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -96,6 +96,7 @@
     <Compile Include="DataUtils\StringUtils\StringGenerator.cs" />
     <Compile Include="DataUtils\StringUtils\StringKeyValParser.cs" />
     <Compile Include="DataUtils\StringUtils\StringTool.cs" />
+    <Compile Include="DataUtils\StringUtils\UrlTool.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertTool.cs" />
     <Compile Include="DbUtils\DbModels\DatabaseType.cs" />
     <Compile Include="DbUtils\DbInterface\IDatabaseHelper.cs" />

+ 16 - 1
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/StringArrayTool.cs

@@ -1,5 +1,7 @@
-using System;
+using Azylee.Core.DataUtils.CollectionUtils;
+using System;
 using System.Collections.Generic;
+using System.Data;
 using System.Linq;
 using System.Text;
 
@@ -15,5 +17,18 @@ namespace Azylee.Core.DataUtils.StringUtils
             }
             return -1;
         }
+        public static string Join(List<string> list, string quotation = "`", string joinChar = ",")
+        {
+            string result = "";
+            if (Ls.ok(list))
+            {
+                for (int i = 0; i < list.Count; i++)
+                {
+                    result += $"{quotation}{list[i]}{quotation}";
+                    if (i < list.Count - 1) result += joinChar;
+                }
+            }
+            return result;
+        }
     }
 }

+ 44 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/UrlTool.cs

@@ -0,0 +1,44 @@
+using Azylee.Core.DataUtils.CollectionUtils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.StringUtils
+{
+    public class UrlTool
+    {
+
+        /// <summary>
+        /// 连接多个string构成url
+        /// </summary>
+        /// <param name="paths"></param>
+        /// <returns></returns>
+        public static string Combine(params string[] paths)
+        {
+            if (ListTool.HasElements(paths))
+            {
+                if (paths.Length > 1)
+                {
+                    StringBuilder result = new StringBuilder(paths[0]);
+                    for (int i = 1; i < paths.Length; i++)
+                    {
+                        if (paths[i] != null)
+                        {
+                            string u = paths[i];
+                            if (u.StartsWith("/")) u = u.Substring(1);
+                            if (!result.ToString().EndsWith("/")) result.Append("/");
+                            result.Append(u);
+                        }
+                    }
+                    return result.ToString();
+                }
+                else
+                {
+                    return paths[0];
+                }
+            }
+            return "";
+        }
+    }
+}

+ 18 - 1
Azylee.Utils/Azylee.Core/IOUtils/TxtUtils/TxtTool.cs

@@ -46,7 +46,7 @@ namespace Azylee.Core.IOUtils.TxtUtils
             catch (Exception e) { }
             return false;
         }
-        public static bool Create(string file, string txt, string encoding="utf-8")
+        public static bool Create(string file, string txt, string encoding = "utf-8")
         {
             try
             {
@@ -61,6 +61,23 @@ namespace Azylee.Core.IOUtils.TxtUtils
             catch (Exception e) { }
             return false;
         }
+        public static bool Create(string file, List<string> txt, string encoding = "utf-8")
+        {
+            try
+            {
+                Encoding enc = Encoding.GetEncoding(encoding);
+                DirTool.Create(Path.GetDirectoryName(file));
+                using (StreamWriter sw = new StreamWriter(file, false, enc))
+                {
+                    if (!ListTool.IsNullOrEmpty(txt))
+                        foreach (var t in txt)
+                            sw.WriteLine(t);
+                }
+                return true;
+            }
+            catch (Exception e) { }
+            return false;
+        }
         public static string Read(string file)
         {
             try

+ 13 - 5
Azylee.Utils/Azylee.WinformSkin/FormUI/Toast/ToastForm.cs

@@ -38,11 +38,15 @@ namespace Azylee.WinformSkin.FormUI.Toast
                 if (type == 'w' || type == 'W') tt = ToastType.warn;
                 if (type == 'e' || type == 'E') tt = ToastType.error;
 
-                form.SetContent(title, text, tt, time);//设置提示框:标题、文本、类型、时间
-                form.ClickAction = clickAction;//设置单击触发事件
+                form.SetContent(title, text, tt, time); //设置提示框:标题、文本、类型、时间
+                form.ClickAction = clickAction; //设置单击触发事件
                 form.Toast();
             }
-            catch { }
+            catch
+            {
+                try { form?.Close(); } catch { }
+                try { form?.Dispose(); } catch { }
+            }
         }
         /// <summary>
         /// 弹出提示框
@@ -63,7 +67,11 @@ namespace Azylee.WinformSkin.FormUI.Toast
                 form.ClickAction = clickAction;//设置单击触发事件
                 form.Toast();
             }
-            catch { }
+            catch
+            {
+                try { form?.Close(); } catch { }
+                try { form?.Dispose(); } catch { }
+            }
         }
 
         private int TimeSpend = 0;
@@ -241,5 +249,5 @@ namespace Azylee.WinformSkin.FormUI.Toast
         //    }
         //} 
         #endregion
-    } 
+    }
 }

+ 7 - 1
Azylee.Utils/Tests/Test.YeahWeb/Program.cs

@@ -15,8 +15,14 @@ namespace Test.YeahWeb
     {
         static void Main(string[] args)
         {
-            testKeyValParser();
+            testUrlCombine();
 
+            Console.ReadLine();
+        }
+
+        public static void testUrlCombine()
+        {
+            Console.WriteLine(UrlTool.Combine("http://www.baidu.com/", "/s/", "/key"));
         }
 
         public static void testKeyValParser()