| | |
| | | using System.Collections.Generic; |
| | | using System.Configuration; |
| | | using System.Data.Common; |
| | | using System.Diagnostics; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | |
| | | { |
| | | public class Tool |
| | | { |
| | | /// <summary> |
| | | /// 基础目录 |
| | | /// </summary> |
| | | public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory; |
| | | |
| | | /// <summary> |
| | | /// 字节格式化 |
| | | /// </summary> |
| | | public static string FormatBytes(long bytes) |
| | | { |
| | | string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" }; |
| | |
| | | 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>(); |
| | |
| | | return list; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建目录 |
| | | /// </summary> |
| | | public static void CreateDirectory(string dir) |
| | | { |
| | | WindowsIdentity wi = WindowsIdentity.GetCurrent(); |
| | |
| | | Directory.CreateDirectory(dir, ds); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 克隆对象 |
| | | /// </summary> |
| | | public static T Clone<T>(T source) where T : new() |
| | | { |
| | | if (!typeof(T).IsSerializable) |
| | |
| | | return (T)formatter.Deserialize(stream); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 执行Python |
| | | /// </summary> |
| | | /// <param name="cmd">命令行</param> |
| | | /// <returns>执行结果或出错信息</returns> |
| | | public static String ExecPython(string cmd) |
| | | { |
| | | string str = null; |
| | | try |
| | | { |
| | | Process 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 so = p.StandardOutput; // 标准输出流 |
| | | StreamReader se = p.StandardError; // 标准错误流 |
| | | |
| | | LogOut.Info("cmd = " + cmd); |
| | | si.AutoFlush = true; |
| | | si.WriteLine("\"C:\\Program Files\\QGIS 3.16\\bin\\python-qgis-ltr.bat\""); |
| | | si.WriteLine(cmd); |
| | | si.WriteLine("exit"); |
| | | |
| | | string info = so.ReadToEnd(); |
| | | str = se.ReadToEnd(); |
| | | |
| | | if (!string.IsNullOrEmpty(info)) LogOut.Debug(info); |
| | | if (!string.IsNullOrEmpty(str)) LogOut.Error(str); |
| | | if (p.HasExited == false) p.Kill(); |
| | | |
| | | se.Close(); |
| | | so.Close(); |
| | | si.Close(); |
| | | p.Close(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.StackTrace); |
| | | str = ex.Message; |
| | | } |
| | | |
| | | return str; |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |