Browse Source

添加ping工具,添加http请求方法类型,添加网卡格式转换,增强httptoolplus工具,添加configtool工具类(操作应用程序配置文件),增强cmd进程工具

yuzhengyang 7 years ago
parent
commit
d096e1dfc7

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


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

@@ -31,6 +31,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
+    <Reference Include="System.configuration" />
     <Reference Include="System.Core" />
     <Reference Include="System.Management" />
     <Reference Include="System.Xml.Linq" />
@@ -81,6 +82,7 @@
     <Compile Include="LogUtils\SimpleLogUtils\LogType.cs" />
     <Compile Include="LogUtils\StatusLogUtils\StatusLogModel.cs" />
     <Compile Include="LogUtils\StatusLogUtils\StatusLog.cs" />
+    <Compile Include="NetUtils\PingTool.cs" />
     <Compile Include="ProcessUtils\ProcessTool.cs" />
     <Compile Include="IOUtils\DirUtils\DirTool.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

+ 57 - 1
Fork.Net/Azylee.Utils/Azylee.Core/IOUtils/TxtUtils/ConfigTool.cs

@@ -1,12 +1,68 @@
 using System;
 using System.Collections.Generic;
+using System.Configuration;
 using System.Linq;
 using System.Text;
 
 namespace Azylee.Core.IOUtils.TxtUtils
 {
-    class ConfigTool
+    public class ConfigTool
     {
+        public static string Get(string key, string defaultValue = "")
+        {
+            try
+            {
+                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
+                return config.AppSettings.Settings[key]?.Value ?? defaultValue;
+            }
+            catch { return defaultValue; }
+        }
+        public static string Get(string exePath, string key, string defaultValue = "")
+        {
+            try
+            {
+                Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
+                return config.AppSettings.Settings[key]?.Value ?? defaultValue;
+            }
+            catch { return defaultValue; }
+        }
+        public static int GetInt(string key, int defaultValue = 0)
+        {
+            string s = Get(key: key);
+            if (int.TryParse(s, out int value)) return value;
+            return defaultValue;
+        }
+        public static int GetInt(string exePath, string key, int defaultValue = 0)
+        {
+            string s = Get(exePath: exePath, key: key);
+            if (int.TryParse(s, out int value)) return value;
+            return defaultValue;
+        }
+
+        public static bool Set(string key, string value)
+        {
+            try
+            {
+                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
+                config.AppSettings.Settings[key].Value = value;
+                config.Save(ConfigurationSaveMode.Modified);
+                ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
+                return true;
+            }
+            catch { return false; }
+        }
+        public static bool Set(string exePath, string key, string value)
+        {
+            try
+            {
+                Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
+                config.AppSettings.Settings[key].Value = value;
+                config.Save(ConfigurationSaveMode.Modified);
+                ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
+                return true;
+            }
+            catch { return false; }
+        }
         //private bool CanUpdate()
         //{
         //    string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";

+ 22 - 0
Fork.Net/Azylee.Utils/Azylee.Core/NetUtils/PingTool.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.NetworkInformation;
+using System.Text;
+
+namespace Azylee.Core.NetUtils
+{
+    public class PingTool
+    {
+        public static bool Ping(string ip)
+        {
+            try
+            {
+                Ping _ping = new Ping();
+                PingReply _reply = _ping.Send(ip);
+                return _reply.Status == IPStatus.Success ? true : false;
+            }
+            catch { return false; }
+        }
+    }
+}

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

@@ -22,12 +22,27 @@ namespace Azylee.Core.WindowsUtils.CMDUtils
         /// <returns></returns>
         public static Process GetProcess()
         {
+            ProcessStartInfo startInfo = new ProcessStartInfo();
+            startInfo.FileName = "cmd.exe";
+            startInfo.Arguments = "/c C:\\Windows\\System32\\cmd.exe";
+            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>
+        /// 创建cmd的进程
+        /// </summary>
+        /// <returns></returns>
+        public static Process GetProcessSimple()
+        {
             Process p = new Process();
             p.StartInfo.FileName = "cmd.exe";
-            p.StartInfo.UseShellExecute = false;
-            p.StartInfo.RedirectStandardError = true;
-            p.StartInfo.RedirectStandardInput = true;
-            p.StartInfo.RedirectStandardOutput = true;
             p.StartInfo.CreateNoWindow = true;
             return p;
         }
@@ -45,6 +60,33 @@ namespace Azylee.Core.WindowsUtils.CMDUtils
             {
                 process = GetProcess();
                 process.Start();
+                process.StandardInput.AutoFlush = true;
+                process.StandardInput.WriteLine(cmd);
+                process.StandardInput.WriteLine("exit");
+                reader = process.StandardOutput;
+                do
+                {
+                    string line = reader.ReadLine();
+                    output?.Invoke(line);
+                } while (!reader.EndOfStream);
+                process.WaitForExit();
+                process.Close();
+            }
+            catch { }
+        }
+        /// <summary>
+        /// 开始运行CMD命令
+        /// </summary>
+        /// <param name="cmd"></param>
+        /// <param name="output">输出动作</param>
+        public static void StartExecuteSimple(string cmd, Action<string> output)
+        {
+            StreamReader reader = null;
+            Process process = null;
+            try
+            {
+                process = GetProcessSimple();
+                process.Start();
                 process.StandardInput.WriteLine(cmd);
                 process.StandardInput.WriteLine("exit");
                 reader = process.StandardOutput;

+ 13 - 0
Fork.Net/Azylee.Utils/Azylee.Core/WindowsUtils/InfoUtils/NetcardInfoTool.cs

@@ -118,6 +118,19 @@ namespace Azylee.Core.WindowsUtils.InfoUtils
         }
 
         /// <summary>
+        /// 全小写MAC地址
+        /// </summary>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public static string ShortMac(string s)
+        {
+            if (!string.IsNullOrWhiteSpace(s))
+            {
+                return s.Replace("-", "").Replace(":", "").ToLower();
+            }
+            return "";
+        }
+        /// <summary>
         /// 格式化MAC地址(大写、':' 分割)
         /// </summary>
         /// <param name="s"></param>

+ 1 - 0
Fork.Net/Azylee.Utils/Azylee.YeahWeb/Azylee.YeahWeb.csproj

@@ -47,6 +47,7 @@
     <Compile Include="BaiDuWebAPI\IPLocationAPI\IPLocationWebModel.cs" />
     <Compile Include="BaiDuWebAPI\IPLocationAPI\IPLocationTool.cs" />
     <Compile Include="FTPUtils\FTPTool.cs" />
+    <Compile Include="HttpUtils\HttpMethodType.cs" />
     <Compile Include="HttpUtils\HttpTool.cs" />
     <Compile Include="HttpUtils\HttpToolPlus.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

+ 13 - 0
Fork.Net/Azylee.Utils/Azylee.YeahWeb/HttpUtils/HttpMethodType.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.YeahWeb.HttpUtils
+{
+    public enum HttpMethodType
+    {
+        Get = 0,
+        Post = 1,
+    }
+}

+ 17 - 10
Fork.Net/Azylee.Utils/Azylee.YeahWeb/HttpUtils/HttpToolPlus.cs

@@ -11,7 +11,7 @@ namespace Azylee.YeahWeb.HttpUtils
     {
         const string DefaultContentType = "application/x-www-form-urlencoded";
         const string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
-        public static string Get(string url, ref CookieCollection cookie, string contentType = DefaultContentType, bool autoRedirect = false, bool keepAlive = true, string userAgent = DefaultUserAgent)
+        public static string Get(string url, ref CookieCollection cookie, Dictionary<string, string> headers = null, string contentType = DefaultContentType, bool autoRedirect = false, bool keepAlive = true, string userAgent = DefaultUserAgent)
         {
             string html = "";
             Stream stream = null;
@@ -25,6 +25,7 @@ namespace Azylee.YeahWeb.HttpUtils
                 request.KeepAlive = keepAlive;
                 request.UserAgent = DefaultUserAgent;
                 request.CookieContainer = new CookieContainer();
+                SetHeaders(ref request, headers);
                 if (cookie != null) request.CookieContainer.Add(cookie);
                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                 response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
@@ -43,7 +44,7 @@ namespace Azylee.YeahWeb.HttpUtils
             }
             return html;
         }
-        public static string Post(string url, ref CookieCollection cookie, Dictionary<string, string> data = null, string contentType = DefaultContentType, bool autoRedirect = true, bool keepAlive = true, string userAgent = DefaultUserAgent)
+        public static string Post(string url, ref CookieCollection cookie, string data = null, Dictionary<string, string> headers = null, string contentType = DefaultContentType, bool autoRedirect = true, bool keepAlive = true, string userAgent = DefaultUserAgent)
         {
             string html = "";
             Stream stream = null, dataStream = null;
@@ -58,18 +59,12 @@ namespace Azylee.YeahWeb.HttpUtils
                 request.KeepAlive = keepAlive;
                 request.UserAgent = DefaultUserAgent;
                 request.CookieContainer = new CookieContainer();
+                SetHeaders(ref request, headers);
                 if (cookie != null) request.CookieContainer.Add(cookie);
                 //配置参数
                 if (data != null)
                 {
-                    StringBuilder dataString = new StringBuilder();
-                    foreach (var item in data)
-                    {
-                        string param = string.Format("&{0}={1}", item.Key, item.Value);
-                        dataString.Append(param);
-                    }
-
-                    byte[] dataByte = Encoding.UTF8.GetBytes(dataString.ToString().Substring(1));
+                    byte[] dataByte = Encoding.UTF8.GetBytes(data);
                     request.ContentLength = dataByte.Length;
                     dataStream = request.GetRequestStream();
                     dataStream.Write(dataByte, 0, dataByte.Length);
@@ -93,5 +88,17 @@ namespace Azylee.YeahWeb.HttpUtils
             }
             return html;
         }
+
+        private static bool SetHeaders(ref HttpWebRequest request, Dictionary<string, string> headers)
+        {
+            try
+            {
+                if (request != null && headers != null && headers.Count > 0)
+                    foreach (var head in headers)
+                        request.Headers.Add(head.Key, head.Value);
+                return true;
+            }
+            catch { return false; }
+        }
     }
 }

+ 16 - 6
Fork.Net/Test/Test.CmdTool/Form1.Designer.cs

@@ -36,11 +36,12 @@
             this.BTStart = new System.Windows.Forms.Button();
             this.TMStatus = new System.Windows.Forms.Timer(this.components);
             this.LBStatus = new System.Windows.Forms.Label();
+            this.TBJar = new System.Windows.Forms.TextBox();
             this.SuspendLayout();
             // 
             // BTFindPort
             // 
-            this.BTFindPort.Location = new System.Drawing.Point(153, 46);
+            this.BTFindPort.Location = new System.Drawing.Point(133, 10);
             this.BTFindPort.Name = "BTFindPort";
             this.BTFindPort.Size = new System.Drawing.Size(120, 23);
             this.BTFindPort.TabIndex = 0;
@@ -50,22 +51,22 @@
             // 
             // TBPort
             // 
-            this.TBPort.Location = new System.Drawing.Point(36, 46);
+            this.TBPort.Location = new System.Drawing.Point(27, 12);
             this.TBPort.Name = "TBPort";
             this.TBPort.Size = new System.Drawing.Size(100, 21);
             this.TBPort.TabIndex = 1;
             // 
             // TBRs
             // 
-            this.TBRs.Location = new System.Drawing.Point(25, 101);
+            this.TBRs.Location = new System.Drawing.Point(25, 48);
             this.TBRs.Multiline = true;
             this.TBRs.Name = "TBRs";
-            this.TBRs.Size = new System.Drawing.Size(631, 253);
+            this.TBRs.Size = new System.Drawing.Size(631, 306);
             this.TBRs.TabIndex = 2;
             // 
             // BTKillApp
             // 
-            this.BTKillApp.Location = new System.Drawing.Point(323, 46);
+            this.BTKillApp.Location = new System.Drawing.Point(259, 10);
             this.BTKillApp.Name = "BTKillApp";
             this.BTKillApp.Size = new System.Drawing.Size(75, 23);
             this.BTKillApp.TabIndex = 3;
@@ -75,7 +76,7 @@
             // 
             // BTStart
             // 
-            this.BTStart.Location = new System.Drawing.Point(493, 46);
+            this.BTStart.Location = new System.Drawing.Point(581, 10);
             this.BTStart.Name = "BTStart";
             this.BTStart.Size = new System.Drawing.Size(75, 23);
             this.BTStart.TabIndex = 4;
@@ -98,11 +99,19 @@
             this.LBStatus.TabIndex = 5;
             this.LBStatus.Text = "label1";
             // 
+            // TBJar
+            // 
+            this.TBJar.Location = new System.Drawing.Point(341, 10);
+            this.TBJar.Name = "TBJar";
+            this.TBJar.Size = new System.Drawing.Size(234, 21);
+            this.TBJar.TabIndex = 6;
+            // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(687, 396);
+            this.Controls.Add(this.TBJar);
             this.Controls.Add(this.LBStatus);
             this.Controls.Add(this.BTStart);
             this.Controls.Add(this.BTKillApp);
@@ -126,6 +135,7 @@
         private System.Windows.Forms.Button BTStart;
         private System.Windows.Forms.Timer TMStatus;
         private System.Windows.Forms.Label LBStatus;
+        private System.Windows.Forms.TextBox TBJar;
     }
 }
 

+ 16 - 1
Fork.Net/Test/Test.CmdTool/Form1.cs

@@ -59,7 +59,11 @@ namespace Test.CmdTool
         {
             Task.Factory.StartNew(() =>
             {
-                //CMDProcessTool.StartExecute(@"java -jar D:\CoCo\Work\supplyPlatform\out\artifacts\noah_cloud_supply_platform_jar\noah-cloud-supply-platform.jar");
+                CMDProcessTool.StartExecute(@"java -jar " + TBJar.Text,
+                    new Action<string>((s) =>
+                    {
+                        UIRs(s);
+                    }));
             });
         }
 
@@ -81,5 +85,16 @@ namespace Test.CmdTool
         {
             LBStatus.Text = $"comcpu:{(int)ComCpu.NextValue()}, appcpu:{(int)AppCpu.NextValue()}, appram:{AppInfoTool.RAM() / 1024}";
         }
+        private void UIRs(string s)
+        {
+            try
+            {
+                Invoke(new Action(() =>
+                {
+                    TBRs.AppendText(s);
+                }));
+            }
+            catch { }
+        }
     }
 }