From 7969eef8a3a2aadaf88672e3c2c5354f5f44f802 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 21 八月 2023 13:49:41 +0800 Subject: [PATCH] 添加工具类 --- ExportMap/cs/Tools.cs | 4 DataLoader/CS/GdalHelper.cs | 4 DataLoader/CS/Tools.cs | 180 +++++++++++++++++++++++++++++++++++++++++++++ DataLoader/DataLoader.csproj | 1 DataLoader/CS/StaticData.cs | 2 5 files changed, 185 insertions(+), 6 deletions(-) diff --git a/DataLoader/CS/GdalHelper.cs b/DataLoader/CS/GdalHelper.cs index a36a1c0..02f508e 100644 --- a/DataLoader/CS/GdalHelper.cs +++ b/DataLoader/CS/GdalHelper.cs @@ -71,10 +71,10 @@ public void RegisterGDal() { - string gdalData = Path.Combine(StaticData.BasePath, "gdal-data"); + string gdalData = Path.Combine(Tools.BaseDir, "gdal-data"); Environment.SetEnvironmentVariable("GDAL_DATA", gdalData); - string proj7 = Path.Combine(StaticData.BasePath, "proj7\\share"); + string proj7 = Path.Combine(Tools.BaseDir, "proj7\\share"); Environment.SetEnvironmentVariable("PROJ_LIB", proj7); Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // NO,YES diff --git a/DataLoader/CS/StaticData.cs b/DataLoader/CS/StaticData.cs index 07e157c..25a11e9 100644 --- a/DataLoader/CS/StaticData.cs +++ b/DataLoader/CS/StaticData.cs @@ -6,8 +6,6 @@ { public class StaticData { - public static String BasePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; - public static String ADMIN = "admin"; public readonly static String ZIP = ".zip"; diff --git a/DataLoader/CS/Tools.cs b/DataLoader/CS/Tools.cs new file mode 100644 index 0000000..d03b1c4 --- /dev/null +++ b/DataLoader/CS/Tools.cs @@ -0,0 +1,180 @@ +锘縰sing Npgsql; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data.Common; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net.Sockets; +using System.Net; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DataLoader.CS +{ + /// <summary> + /// 宸ュ叿绫� + /// </summary> + public class Tools + { + /// <summary> + /// 鍩虹鐩綍 + /// </summary> + public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory; + + private static PostgreHelper _dbHelper; + + /// <summary> + /// DB甯姪绫� + /// </summary> + public static PostgreHelper DBHelper + { + get + { + if (null == _dbHelper) + { + _dbHelper = new PostgreHelper(); + } + + return _dbHelper; + } + } + + /// <summary> + /// 瀛楄妭鏍煎紡鍖� + /// </summary> + public static string FormatBytes(long bytes) + { + string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" }; + + int i = 0; + double dblSByte = bytes; + if (bytes > 1024) + for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024) + dblSByte = bytes / 1024.0; + + return String.Format("{0:0.##}{1}", dblSByte, Suffix[i]); + } + + /// <summary> + /// 鑾峰彇璁剧疆 + /// </summary> + public static string GetSetting(string key) + { + return ConfigurationManager.AppSettings[key]; + } + + /// <summary> + /// 鑾峰彇Db鍙傛暟 + /// </summary> + public static List<DbParameter> GetParams<T>(string sql, T t) + { + List<DbParameter> list = new List<DbParameter>(); + Type tType = typeof(T); + BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; + + int start = sql.IndexOf("@"); + while (start != -1) + { + int end = sql.IndexOf(",", start); + if (end == -1) end = sql.IndexOf(")", start); + if (end == -1) end = sql.IndexOf(" ", start); + if (end == -1) end = sql.Length; + + string name = sql.Substring(start + 1, end - start - 1); + PropertyInfo pi = tType.GetProperty(name, flags); + if (pi != null) + { + object value = pi.GetValue(t, null); + DbParameter dp = new NpgsqlParameter("@" + name, value); + list.Add(dp); + } + + start = sql.IndexOf("@", end); + } + + return list; + } + + /// <summary> + /// 鎵ц鍛戒护 + /// </summary> + /// <param name="list">鍛戒护闆嗗悎</param> + public static string ExecCmd(List<string> list) + { + string str = null; + Process p = null; + try + { + p = new Process(); + p.StartInfo.FileName = "cmd.exe"; + p.StartInfo.UseShellExecute = false; + p.StartInfo.CreateNoWindow = true; + p.StartInfo.RedirectStandardInput = true; + p.StartInfo.RedirectStandardOutput = true; + p.StartInfo.RedirectStandardError = true; + p.Start(); + + StreamWriter si = p.StandardInput; + StreamReader se = p.StandardError; + + LogOut.Info("cmd = " + string.Join("锛�", list)); + si.AutoFlush = true; + foreach (string cmd in list) + { + si.WriteLine(cmd); + } + si.WriteLine("exit"); + + str = se.ReadToEnd(); + se.Close(); + si.Close(); + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + str = ex.Message; + } + finally + { + if (p != null) + { + p.Close(); + p = null; + } + } + return str; + } + + /// <summary> + /// 鍒犻櫎璺緞 + /// </summary> + public static string DelPath(string path) + { + List<string> list = new List<string>(); + list.Add(string.Format("rd \"{0}\" /s /q", path)); + + return ExecCmd(list); + } + + /// <summary> + /// 鑾峰彇鏈満IP + /// </summary> + /// <returns></returns> + public static string GetLocalIP() + { + var host = Dns.GetHostEntry(Dns.GetHostName()); + foreach (var ip in host.AddressList) + { + if (ip.AddressFamily == AddressFamily.InterNetwork) + { + return ip.ToString(); + } + } + + return GetSetting("localIP"); + } + } +} diff --git a/DataLoader/DataLoader.csproj b/DataLoader/DataLoader.csproj index 5f2a575..5750e0f 100644 --- a/DataLoader/DataLoader.csproj +++ b/DataLoader/DataLoader.csproj @@ -136,6 +136,7 @@ <Compile Include="CS\LogOut.cs" /> <Compile Include="CS\MD5Helper.cs" /> <Compile Include="CS\StaticData.cs" /> + <Compile Include="CS\Tools.cs" /> <Compile Include="Model\Tool.cs" /> <Compile Include="CS\ModelHandler.cs" /> <Compile Include="Model\NtstWeb.cs" /> diff --git a/ExportMap/cs/Tools.cs b/ExportMap/cs/Tools.cs index 814e34a..4603d02 100644 --- a/ExportMap/cs/Tools.cs +++ b/ExportMap/cs/Tools.cs @@ -124,7 +124,7 @@ } /// <summary> - /// 鍒涘缓鐩綍 + /// 鍒涘缓鐩綍 * /// </summary> public static void CreateDirectory(string dir) { @@ -138,7 +138,7 @@ } /// <summary> - /// 鍏嬮殕瀵硅薄 + /// 鍏嬮殕瀵硅薄 * /// </summary> public static T Clone<T>(T source) where T : new() { -- Gitblit v1.9.3