12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace DbMigrate
- {
- internal static class Program
- {
- static Program()
- {
- //设置私有路径
- Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
- AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "AddIns;");
- var m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
- var funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
- m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", "AddIns;" });
- //c++dll加入环境变量
- if (Directory.Exists("AddIns"))
- {
- string paths = Environment.GetEnvironmentVariable("PATH");
- var dirs = Directory.EnumerateDirectories("AddIns", "*", SearchOption.AllDirectories);
- List<string> list = new List<string>
- {
- Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns")
- };
- foreach (var item in dirs)
- {
- list.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item));
- }
- Environment.SetEnvironmentVariable("PATH", $"{paths};{string.Join(";", list)}");
- }
- AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
- {
- var ex = e.ExceptionObject as Exception;
- while (ex.InnerException != null)
- ex = ex.InnerException;
- MessageBox.Show("出现未处理的异常,程序即将退出!");
- };
- Application.ThreadException += (sender, e) =>
- {
- var ex = e.Exception;
- while (ex.InnerException != null)
- ex = ex.InnerException;
- MessageBox.Show($"出现未处理的线程异常!{e.Exception.Message}");
- };
- AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
- }
- private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
- {
- var name = args.Name.Split(',')[0];
- var dll1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns", $"{name}.dll");
- if (File.Exists(dll1))
- return Assembly.Load(dll1);
- var dll2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{name}.dll");
- if (File.Exists(dll2))
- return Assembly.Load(dll2);
- return null;
- }
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- }
- }
- }
|