ソースを参照

新增控制台输出组件、修改列表及文件工具等

yuzhengyang 9 年 前
コミット
cdd2099cc6

+ 0 - 6
Fork.Net/Y.Utils/BaseUtils/Class1.cs

@@ -1,6 +0,0 @@
-namespace Y.Utils.BaseUtils
-{
-    class Class1
-    {
-    }
-}

+ 4 - 0
Fork.Net/Y.Utils/BaseUtils/ListTool.cs

@@ -23,5 +23,9 @@ namespace Y.Utils.BaseUtils
                 return false;
             return true;
         }
+        public static bool HasElements<T>(IEnumerable<T> list)
+        {
+            return !IsNullOrEmpty(list);
+        }
     }
 }

+ 55 - 2
Fork.Net/Y.Utils/ComputerUtils/ComputerTool.cs

@@ -1,9 +1,13 @@
-using System.Management;
+using System;
+using System.Collections.Generic;
+using System.Management;
+using System.Net.NetworkInformation;
 
 namespace Y.Utils.ComputerUtils
 {
-    public class ComputerTool
+    public static class ComputerInfoTool
     {
+        #region 获取CpuId
         public static string GetCpuId()
         {
             ManagementClass mc = null;
@@ -29,5 +33,54 @@ namespace Y.Utils.ComputerUtils
                 if (mc != null) mc.Dispose();
             }
         }
+        #endregion
+        #region 获取CPU信息
+        public static string GetCpuInfo()
+        {
+            try
+            {
+                string result = "";
+                ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from  Win32_Processor");
+                foreach (ManagementObject item in searcher.Get())
+                {
+                    result = item["Name"].ToString();
+                }
+                return result;
+            }
+            catch
+            { return "unknown"; }
+        }
+        #endregion
+
+        #region 获取网卡信息
+        /// <summary>
+        /// 获取网卡信息
+        /// Item1:描述,Item2:物理地址(Mac),Item3:Ip地址
+        /// </summary>
+        /// <returns></returns>
+        public static List<Tuple<string, string, string>> GetNetworkCardInfo()
+        {
+            try
+            {
+                List<Tuple<string, string, string>> result = new List<Tuple<string, string, string>>();
+                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
+                foreach (var item in adapters)
+                {
+                    if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
+                    {
+                        result.Add(new Tuple<string, string, string>(
+                            item.Description,
+                            item.GetPhysicalAddress().ToString(),
+                            item.GetIPProperties().UnicastAddresses[1].Address.ToString()));
+                    }
+                }
+                return result;
+            }
+            catch (NetworkInformationException e)
+            {
+                return null;
+            }
+        }
+        #endregion
     }
 }

+ 68 - 2
Fork.Net/Y.Utils/FileUtils/FileTool.cs

@@ -8,6 +8,12 @@ namespace Y.Utils.FileUtils
 {
     public class FileTool
     {
+        /// <summary>
+        /// 获取文件(单层目录)
+        /// </summary>
+        /// <param name="path">路径</param>
+        /// <param name="pattern">通配符</param>
+        /// <returns></returns>
         public static List<string> GetFile(string path, string pattern = "*")
         {
             if (Directory.Exists(path))
@@ -19,6 +25,12 @@ namespace Y.Utils.FileUtils
                 catch (Exception e) { }
             return null;
         }
+        /// <summary>
+        /// 获取文件(所有目录)
+        /// </summary>
+        /// <param name="path">路径</param>
+        /// <param name="pattern">通配符</param>
+        /// <returns></returns>
         public static List<string> GetAllFile(string path, string pattern = "*")
         {
             List<string> result = null;
@@ -29,6 +41,12 @@ namespace Y.Utils.FileUtils
             catch (Exception e) { }
             return result;
         }
+        /// <summary>
+        /// 获取文件(所有目录)
+        /// </summary>
+        /// <param name="path">路径</param>
+        /// <param name="pattern">通配符(支持多个通配符)</param>
+        /// <returns></returns>
         public static List<string> GetAllFile(string path, string[] pattern)
         {
             List<string> result = new List<string>();
@@ -42,12 +60,18 @@ namespace Y.Utils.FileUtils
             }
             return result;
         }
+        /// <summary>
+        /// 获取文件(所有目录)
+        /// </summary>
+        /// <param name="paths">路径(支持多个路径)</param>
+        /// <param name="patterns">通配符(支持多个通配符)</param>
+        /// <returns></returns>
         public static List<string> GetAllFile(string[] paths, string[] patterns)
         {
             List<string> result = new List<string>();
             if (!ListTool.IsNullOrEmpty(paths))
             {
-                foreach(var path in paths)
+                foreach (var path in paths)
                 {
                     if (!ListTool.IsNullOrEmpty(patterns))
                     {
@@ -56,7 +80,8 @@ namespace Y.Utils.FileUtils
                             List<string> temp = GetAllFile(path, pattern);
                             if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
                         }
-                    }else
+                    }
+                    else
                     {
                         List<string> temp = GetAllFile(path);
                         if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
@@ -65,6 +90,42 @@ namespace Y.Utils.FileUtils
             }
             return result;
         }
+        /// <summary>
+        /// 获取文件(所有目录)(严格模式:从第一个.开始截取后缀)
+        /// </summary>
+        /// <param name="paths">路径(支持多个路径)</param>
+        /// <param name="patterns">通配符(支持多个通配符)</param>
+        /// <returns></returns>
+        public static List<string> GetAllFileByExt(string[] paths, string[] patterns)
+        {
+            List<string> result = new List<string>();
+            if (!ListTool.IsNullOrEmpty(paths))
+            {
+                foreach (var path in paths)
+                {
+                    List<string> temp = GetAllFile(path);
+                    if (!ListTool.IsNullOrEmpty(temp)) result.AddRange(temp);
+                }
+            }
+            if (!ListTool.IsNullOrEmpty(patterns) && !ListTool.IsNullOrEmpty(result))
+            {
+                for (int i = result.Count() - 1; i >= 0; i--)
+                {
+                    string ext = Path.GetFileName(result[i]);
+                    if (ext.IndexOf('.') >= 0)
+                    {
+                        ext = ext.Substring(ext.IndexOf('.'));
+                    }
+                    if (!patterns.Contains(ext)) result.RemoveAt(i);
+                }
+            }
+            return result;
+        }
+        /// <summary>
+        /// 删除文件
+        /// </summary>
+        /// <param name="file">文件路径</param>
+        /// <returns></returns>
         public static bool Delete(string file)
         {
             try
@@ -82,6 +143,11 @@ namespace Y.Utils.FileUtils
             catch { }
             return false;
         }
+        /// <summary>
+        /// 删除文件(多个)
+        /// </summary>
+        /// <param name="files">文件路径(支持多个文件路径)</param>
+        /// <returns></returns>
         public static bool Delete(string[] files)
         {
             bool result = true;

+ 126 - 0
Fork.Net/Y.Utils/LogUtils/Log.cs

@@ -0,0 +1,126 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Y.Utils.LogUtils
+{
+    /// <summary>
+    /// Log 输出工具
+    /// 
+    /// 说明:
+    /// 1、Log.AllocConsole();开启控制台
+    /// 2、Log.FreeConsole();关闭控制台
+    /// 3、Log.i("information");输出消息
+    /// </summary>
+    public class Log
+    {
+        //输出的 Log 格式
+        const string LogFormat = "{0}   {1}   {2}";
+        const string TimeFormat = "MM-dd HH:mm:ss.fff";
+
+        #region 输出类型
+        /// <summary>
+        /// 输出类型
+        /// </summary>
+        enum PrintType
+        {
+            v,//verbose 啰嗦的意思
+            d,//debug 调试的信息
+            i,//information 一般提示性的消息
+            w,//warning 警告
+            e,//error 错误信息
+        }
+        #endregion
+        #region Console 开启/关闭 API
+        [DllImport("kernel32.dll")]
+        public static extern Boolean AllocConsole();
+        [DllImport("kernel32.dll")]
+        public static extern Boolean FreeConsole();
+        #endregion
+        #region 输出颜色
+        /// <summary>
+        /// 获取输出颜色
+        /// </summary>
+        /// <param name="type">输出类型</param>
+        /// <returns></returns>
+        private static ConsoleColor GetColor(PrintType type)
+        {
+            switch (type)
+            {
+                case PrintType.v: return ConsoleColor.Gray;
+                case PrintType.d: return ConsoleColor.Blue;
+                case PrintType.i: return ConsoleColor.Green;
+                case PrintType.w: return ConsoleColor.Yellow;
+                case PrintType.e: return ConsoleColor.Red;
+                default: return ConsoleColor.Gray;
+            }
+        }
+        #endregion
+        #region 写出 Log
+        /// <summary>
+        /// 写出到控制台
+        /// </summary>
+        /// <param name="type">类型</param>
+        /// <param name="tag">标记</param>
+        /// <param name="message">消息</param>
+        private static void Write(PrintType type, string message)
+        {
+            DateTime now = DateTime.Now;
+            Console.ForegroundColor = GetColor(type);
+            Console.WriteLine(LogFormat, now.ToString(TimeFormat), type.ToString(), message);
+        }
+        #endregion
+
+        #region 分类详细输出
+        /// <summary>
+        /// 输出 verbose (啰嗦信息)
+        /// </summary>
+        /// <param name="message">消息</param>
+        /// <param name="tag">可选:标记</param>
+        public static void v(string message)
+        {
+            Write(PrintType.v, message);
+        }
+        /// <summary>
+        /// 输出 Debug (调试信息)
+        /// </summary>
+        /// <param name="message">消息</param>
+        /// <param name="tag">可选:标记</param>
+        public static void d(string message)
+        {
+            Write(PrintType.d, message);
+        }
+        /// <summary>
+        /// 输出 Information (重要信息)
+        /// </summary>
+        /// <param name="message">消息</param>
+        /// <param name="tag">可选:标记</param>
+        public static void i(string message)
+        {
+            Write(PrintType.i, message);
+        }
+        /// <summary>
+        /// 输出 Warning (警告信息)
+        /// </summary>
+        /// <param name="message">消息</param>
+        /// <param name="tag">可选:标记</param>
+        public static void w(string message)
+        {
+            Write(PrintType.w, message);
+        }
+        /// <summary>
+        /// 输出 Error (错误信息)
+        /// </summary>
+        /// <param name="message">消息</param>
+        /// <param name="tag">可选:标记</param>
+        public static void e(string message)
+        {
+            Write(PrintType.e, message);
+        }
+        #endregion
+    }
+
+}

+ 1 - 1
Fork.Net/Y.Utils/Properties/AssemblyInfo.cs

@@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
 // 方法是按如下所示使用“*”: :
 // [assembly: AssemblyVersion("1.0.*")]
 [assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.1.0.0")]

+ 1 - 1
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -54,7 +54,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AppUtils\UniqueTool.cs" />
-    <Compile Include="BaseUtils\Class1.cs" />
+    <Compile Include="LogUtils\Log.cs" />
     <Compile Include="ComputerUtils\ComputerTool.cs" />
     <Compile Include="ComputerUtils\ComputerPermissionTool.cs" />
     <Compile Include="ComputerUtils\RegisterTool.cs" />