管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-03-13 2f38d9ee5f607a1078258a8575acf9600b9e8595
ExportMap/cs/Tool.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -16,8 +17,14 @@
{
    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" };
@@ -31,11 +38,17 @@
            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>();
@@ -65,6 +78,9 @@
            return list;
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        public static void CreateDirectory(string dir)
        {
            WindowsIdentity wi = WindowsIdentity.GetCurrent();
@@ -76,6 +92,9 @@
            Directory.CreateDirectory(dir, ds);
        }
        /// <summary>
        /// 克隆对象
        /// </summary>
        public static T Clone<T>(T source) where T : new()
        {
            if (!typeof(T).IsSerializable)
@@ -98,5 +117,56 @@
                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;
        }
    }
}
}