¶Ô±ÈÐÂÎļþ |
| | |
| | | using 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"); |
| | | } |
| | | } |
| | | } |