FrmLoading.cs 5.4 KB
using BLL;
using Model;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

namespace SmartScan
{
    public partial class FrmLoading : Asa.FaceControl.FormBase
    {
        private readonly bool back;

        public FrmLoading(bool back)
        {
            this.back = back;
            InitializeComponent();
        }

        private void Init()
        {
            Process current = Process.GetCurrentProcess();
            FileInfo fileInfo = new FileInfo(current.MainModule.FileName);         
            LogNet.log = log4net.LogManager.GetLogger("SmartScan");
            //DateTime dateTime = fileInfo.LastWriteTime;
            //string version = $"{dateTime.Year%10}.{dateTime.Month}.{dateTime.Day.ToString("00")}{dateTime.Hour.ToString("00")}";
            string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            LogNet.log.Info($"===== 程序开始 {version} =====");

            BLLCommon.config = new BLL.Config();
            BLLCommon.config.SoftVersion = version;
            //BLL.Config.Backgrounder = back;
            ExtraFileData.Init();
            BLLCommon.extraKey = ExtraFileData.Titles;
            Asa.FaceControl.Language.LoadPath(FilePath.LANGUAGE_DIR);
            LogNet.log.Debug("加载语言文件夹:" + FilePath.LANGUAGE_DIR);
            Asa.FaceControl.Language.LoadLanguage(BLLCommon.config.Language);
            Camera.LoadCamera();

            if (BLLCommon.config.EnabledIO)
            {
                string ip = BLLCommon.config.IOIPAddress;
                BLLCommon.ioModule = new BLL.IOManage(ip, BLLCommon.config.iomodule);
                BLLCommon.ioModule.Connect();
                LogNet.log.Info($"加载IO模块,IP地址:{ip},iomodule:{BLLCommon.config.iomodule}");
            }

            if (Config.Func_EnabledOCR)
            {
                LogNet.log.Info("启用OCR模块");
            }

            BLLCommon.lightSource = new();
            BLLCommon.extension = new(BLLCommon.config);
            BLLCommon.labelEdit = new();
            BLLCommon.mateEdit = new();
            BLLCommon.neximApiUtils = new();
            //如果是直接启动ns100就不需要开启条码规则服务
            //WebService.Open();
            ReadMacro();
            Shortcut();

            Invoke(new Action(() => { Close(); }));
        }

        private void ReadMacro()
        {
            LogNet.log.Info("读取关键字文件");
            LogNet.log.Debug("关键字文件路径:" + FilePath.CONFIG_MACRO_KEY);
            BLLCommon.macroKey = new();
            BLLCommon.macroKeyValue = new();

            if (System.IO.File.Exists(FilePath.CONFIG_MACRO_KEY))
            {
                string[] lines = System.IO.File.ReadAllLines(FilePath.CONFIG_MACRO_KEY);
                BLLCommon.macroKey.AddRange(lines);
                LogNet.log.Debug($"添加关键字 {lines.Length} 行");
            }
            else
            {
                LogNet.log.Debug("关键字文件路径不存在");
            }
            if (System.IO.File.Exists(FilePath.CONFIG_MACRO_Value))
            {
                string[] lines = System.IO.File.ReadAllLines(FilePath.CONFIG_MACRO_Value);
                BLLCommon.macroKeyValue.AddRange(lines);
                LogNet.log.Debug($"添加关键字 {lines.Length} 行");
            }
            else
            {
                LogNet.log.Debug("关键字文件路径不存在");
            }
        }

        private void Shortcut()
        {
            if (!BLLCommon.config.CheckShortcut) return;
            string name = System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath);
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + string.Format("\\{0}.lnk", name);
            if (System.IO.File.Exists(path)) return;

            //向cmd窗口发送输入信息
            string str = Application.StartupPath + "\\myshortcut.vbs";
            if (!System.IO.File.Exists(str)) return;
            str = "\"" + str + "\"";
            str += " /shortcut:" + name;
            str += " /target:\"" + Application.ExecutablePath + "\"";
            str += " /directory:\"" + Application.StartupPath + "\"";
            str += " /description:" + string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            str += "&exit";

            System.Diagnostics.Process process = new();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;       //是否使用操作系统shell启动
            process.StartInfo.RedirectStandardInput = true;  //接受来自调用程序的输入信息
            process.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
            process.StartInfo.RedirectStandardError = true;  //重定向标准错误输出
            process.StartInfo.CreateNoWindow = true;         //不显示程序窗口
            process.Start();
            process.StandardInput.WriteLine(str);
            process.StandardInput.AutoFlush = true;
            process.WaitForExit();
            process.Close();
        }

        private void FrmLoading_Load(object sender, EventArgs e)
        {
            if (!back) faceLoading1.Start();
            new Thread(new ThreadStart(() => Init())).Start();
        }

        private void FrmLoading_Activated(object sender, EventArgs e)
        {
            if (back) Hide();
        }
    }
}