Browse Source

合并对进程、CMD进程工具的修改

yuzhengyang 7 years ago
parent
commit
9795abe09f

BIN
Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide


BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide


BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide-shm


BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide-wal


+ 3 - 0
Fork.Net/Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -87,9 +87,12 @@
     <Compile Include="NetUtils\IPFormatter.cs" />
     <Compile Include="NetUtils\MacFormatter.cs" />
     <Compile Include="NetUtils\PingTool.cs" />
+    <Compile Include="ProcessUtils\ProcessStarter.cs" />
     <Compile Include="ProcessUtils\ProcessTool.cs" />
     <Compile Include="IOUtils\DirUtils\DirTool.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="ProxyUtils\SimpleProxyUtils\RunMode.cs" />
+    <Compile Include="ProxyUtils\SimpleProxyUtils\SimpleProxyTool.cs" />
     <Compile Include="Readme.cs" />
     <Compile Include="TaskUtils\TaskSupport.cs" />
     <Compile Include="ThreadUtils\SleepUtils\Sleep.cs" />

+ 55 - 0
Fork.Net/Azylee.Utils/Azylee.Core/ProcessUtils/ProcessStarter.cs

@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.ProcessUtils
+{
+    public static class ProcessStarter
+    {
+        /// <summary>
+        /// 创建进程
+        /// </summary>
+        /// <returns></returns>
+        public static Process NewProcess(string exe, string args = "")
+        {
+            ProcessStartInfo startInfo = new ProcessStartInfo();
+            startInfo.FileName = exe;
+            startInfo.Arguments = args;
+            startInfo.RedirectStandardInput = true;
+            startInfo.RedirectStandardOutput = true;
+            startInfo.RedirectStandardError = true;
+            startInfo.UseShellExecute = false;
+            startInfo.CreateNoWindow = true;
+            startInfo.Verb = "RunAs";
+            Process process = new Process();
+            process.StartInfo = startInfo;
+            return process;
+        }
+        /// <summary>
+        /// 开始运行
+        /// </summary>
+        /// <param name="process"></param>
+        /// <param name="output"></param>
+        public static void Execute(Process process, Action<string> output)
+        {
+            StreamReader reader = null;
+            try
+            {
+                process.Start();
+                process.StandardInput.AutoFlush = true;
+                reader = process.StandardOutput;
+                do
+                {
+                    string line = reader.ReadLine();
+                    output?.Invoke(line);
+                } while (!reader.EndOfStream);
+                process.WaitForExit();
+                process.Close();
+            }
+            catch { }
+        }
+    }
+}

+ 60 - 10
Fork.Net/Azylee.Utils/Azylee.Core/ProcessUtils/ProcessTool.cs

@@ -1,14 +1,16 @@
 //************************************************************************
 //      https://github.com/yuzhengyang
 //      author:     yuzhengyang
-//      date:       2017.10.12 - 2018.5.02
+//      date:       2017.10.12 - 2018.5.28
 //      desc:       进程工具
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************
 using Azylee.Core.DataUtils.CollectionUtils;
+using Azylee.Core.ThreadUtils.SleepUtils;
 using System;
 using System.Diagnostics;
 using System.IO;
+using System.Threading.Tasks;
 
 namespace Azylee.Core.ProcessUtils
 {
@@ -90,17 +92,14 @@ namespace Azylee.Core.ProcessUtils
         /// <summary>
         /// 停止当前进程
         /// </summary>
-        public static void KillCurrentProcess()
+        public static void KillCurrent()
         {
-            Process current = Process.GetCurrentProcess();
-            Process[] processes = Process.GetProcessesByName(current.ProcessName);
-            foreach (Process process in processes)
+            try
             {
-                if (process.Id == current.Id)
-                {
-                    process.Kill();
-                }
+                Process current = Process.GetCurrentProcess();
+                current.Kill();
             }
+            catch { }
         }
         /// <summary>
         /// 停止多个进程
@@ -117,7 +116,58 @@ namespace Azylee.Core.ProcessUtils
                 }
             }
         }
-
+        /// <summary>
+        /// 停止超时进程
+        /// </summary>
+        /// <param name="name">进程名(不含后缀)</param>
+        /// <param name="file">文件路径(为空则不验证)</param>
+        /// <param name="second">超时时间(单位:秒)</param>
+        public static void Kills(string name, string file, int second)
+        {
+            try
+            {
+                Process[] list = Process.GetProcessesByName(name);
+                if (ListTool.HasElements(list))
+                {
+                    DateTime time = DateTime.Now.AddSeconds(-1 * second);
+                    foreach (var p in list)
+                    {
+                        if (file == null || p.MainModule.FileName == file)
+                            if (p.StartTime < time)
+                                try { p.Kill(); } catch { }
+                    }
+                }
+            }
+            catch { }
+        }
+        /// <summary>
+        /// 强制关闭超时进程
+        /// </summary>
+        /// <param name="process"></param>
+        /// <param name="second"></param>
+        public static void Kill(Process process, int second)
+        {
+            DateTime time = DateTime.Now.AddSeconds(-1 * second);
+            if (process.StartTime < time)
+                try { process.Kill(); } catch { }
+        }
+        /// <summary>
+        /// 延时并关闭进程
+        /// </summary>
+        /// <param name="process"></param>
+        /// <param name="second"></param>
+        public static void SleepKill(Process process, short second)
+        {
+            Task.Factory.StartNew(() =>
+            {
+                try
+                {
+                    Sleep.S(second);
+                    process?.Kill();
+                }
+                catch { }
+            });
+        }
         /// <summary>
         /// 根据PID获取InstanceName(不要用于性能计数器,#1..实例名会自动改变)
         /// </summary>

+ 15 - 0
Fork.Net/Azylee.Utils/Azylee.Core/ProxyUtils/SimpleProxyUtils/RunMode.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.ProxyUtils.SimpleProxyUtils
+{ 
+    public enum RunMode
+    {
+        AllBefore,
+        AllAfter,
+        MethodBefore,
+        MethodAfter
+    }
+}

+ 91 - 0
Fork.Net/Azylee.Utils/Azylee.Core/ProxyUtils/SimpleProxyUtils/SimpleProxyTool.cs

@@ -0,0 +1,91 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+
+namespace Azylee.Core.ProxyUtils.SimpleProxyUtils
+{
+    public class SimpleProxyTool<T>
+    {
+        T Object;
+        /// <summary>
+        /// 记录方法前置操作和后置操作
+        /// </summary>
+        List<Tuple<int, RunMode, string, Action>> Operation = new List<Tuple<int, RunMode, string, Action>>();
+
+        public SimpleProxyTool(T obj)
+        {
+            Object = obj;
+        }
+        /// <summary>
+        /// 添加操作
+        /// </summary>
+        /// <param name="type">执行类型</param>
+        /// <param name="method">方法名</param>
+        /// <param name="action">动作</param>
+        public void Add(RunMode type, string method, Action action)
+        {
+            Operation.Add(new Tuple<int, RunMode, string, Action>(Operation.Count, type, method, action));
+        }
+        /// <summary>
+        /// 执行方法
+        /// </summary>
+        /// <typeparam name="R">返回值</typeparam>
+        /// <param name="methodName">方法名</param>
+        /// <param name="objs">参数</param>
+        /// <returns></returns>
+        public R Invoke<R>(string methodName, object[] objs)
+        {
+            //执行全局前置操作
+            List<Tuple<int, RunMode, string, Action>> allBefore = Operation.Where(x => x.Item2 == RunMode.AllBefore).ToList();
+            if (allBefore != null) allBefore.ForEach(b => { b.Item4?.Invoke(); });
+
+            //执行方法前置操作
+            List<Tuple<int, RunMode, string, Action>> methodBefore = Operation.Where(x => x.Item3 == methodName && x.Item2 == RunMode.MethodBefore).ToList();
+            if (methodBefore != null) methodBefore.ForEach(b => { b.Item4?.Invoke(); });
+
+            MethodInfo method = Object.GetType().GetMethod(methodName);
+            object rs = method.Invoke(Object, objs);
+
+            //执行方法后置操作
+            List<Tuple<int, RunMode, string, Action>> methodAfter = Operation.Where(x => x.Item3 == methodName && x.Item2 == RunMode.MethodAfter).ToList();
+            if (methodAfter != null) methodAfter.ForEach(b => { b.Item4?.Invoke(); });
+
+            //执行全局后置操作
+            List<Tuple<int, RunMode, string, Action>> allAfter = Operation.Where(x => x.Item2 == RunMode.AllAfter).ToList();
+            if (allAfter != null) allAfter.ForEach(b => { b.Item4?.Invoke(); });
+
+            return (R)rs;
+        }
+    }
+
+    public class Dog
+    {
+        public string Jump(string name) { return name + " Jump"; }
+        public string Play(string name) { return name + " Play"; }
+    }
+    class Test
+    {
+        private void Main()
+        {
+            //新建对象
+            Dog dog = new Dog();
+            //新建代理
+            SimpleProxyTool<Dog> proxy = new SimpleProxyTool<Dog>(dog);
+            //初始化代理前置、后置操作
+            proxy.Add(RunMode.MethodBefore, "Jump", new Action(() => { Console.WriteLine("跳之前1"); }));
+            proxy.Add(RunMode.MethodBefore, "Jump", new Action(() => { Console.WriteLine("跳之前2"); }));
+            proxy.Add(RunMode.MethodAfter, "Jump", new Action(() => { Console.WriteLine("跳之后1"); }));
+            proxy.Add(RunMode.MethodAfter, "Jump", new Action(() => { Console.WriteLine("跳之后2"); }));
+
+            proxy.Add(RunMode.MethodBefore, "Play", new Action(() => { Console.WriteLine("Play之前"); }));
+            proxy.Add(RunMode.MethodAfter, "Play", new Action(() => { Console.WriteLine("Play之后"); }));
+
+            proxy.Add(RunMode.AllBefore, "", new Action(() => { Console.WriteLine("所有方法之前"); }));
+
+            //执行目标方法
+            string rs = proxy.Invoke<string>("Jump", new[] { "Tom" });
+        }
+    }
+}

+ 2 - 2
Fork.Net/Azylee.Utils/Azylee.Core/ThreadUtils/SleepUtils/SleepTool.cs

@@ -14,14 +14,14 @@ namespace Azylee.Core.ThreadUtils.SleepUtils
         /// </summary>
         public static void Zs(short s = 1)
         {
-            Thread.Sleep(s * 1000);
+            try { Thread.Sleep(s * 1000); } catch { }
         }
         /// <summary>
         /// Sleep(单位:分)
         /// </summary>
         public static void Zm(short m = 1)
         {
-            Thread.Sleep(m * 60 * 1000);
+            try { Thread.Sleep(m * 60 * 1000); } catch { }
         }
     }
 }

+ 4 - 4
Fork.Net/Azylee.Utils/Azylee.Core/WindowsUtils/CMDUtils/CMDProcessTool.cs

@@ -1,17 +1,17 @@
 //************************************************************************
 //      https://github.com/yuzhengyang
 //      author:     yuzhengyang
-//      date:       2018.4.27 - 2018.4.27
+//      date:       2018.4.27 - 2018.5.30
 //      desc:       CMD 工具
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************
 using Azylee.Core.DataUtils.StringUtils;
+using Azylee.Core.ThreadUtils.SleepUtils;
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
-using System.Linq;
-using System.Text;
+using System.Threading.Tasks;
 
 namespace Azylee.Core.WindowsUtils.CMDUtils
 {
@@ -64,7 +64,7 @@ namespace Azylee.Core.WindowsUtils.CMDUtils
             catch { }
         }
         /// <summary>
-        /// 一次性运行CMD并读取结果(建议执行返回数据较小的命令)
+        /// 运行CMD并读取结果(建议执行返回数据较小的命令)
         /// </summary>
         /// <param name="cmd"></param>
         /// <returns></returns>

+ 5 - 2
Fork.Net/Azylee.Utils/Azylee.Core/WindowsUtils/InfoUtils/NetcardInfoTool.cs

@@ -36,8 +36,11 @@ namespace Azylee.Core.WindowsUtils.InfoUtils
                         string _name = item.Name.Trim();
                         string _desc = item.Description.Trim();
                         string _mac = item.GetPhysicalAddress().ToString();
-                        string _ip = item.GetIPProperties().UnicastAddresses.Count >= 2 ?
-                            item.GetIPProperties().UnicastAddresses[1].Address.ToString() : null;
+                        string _ip = item.GetIPProperties().UnicastAddresses.Count >= 1 ?
+                            item.GetIPProperties().UnicastAddresses[0].Address.ToString() : null;
+                        //更新IP为ipv4地址
+                        _ip = item.GetIPProperties().UnicastAddresses.Count >= 2 ?
+                          item.GetIPProperties().UnicastAddresses[1].Address.ToString() : null;
                         string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
                             item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
                         result.Add(new Tuple<string, string, string, string, string>(_name, _desc, _mac, _ip, _gateway));