/// <summary> /// Installs this instance. /// </summary> public static void Install() { FieldInfo[] fiStateValues = null; Type tInitState = typeof(System.Configuration.ConfigurationManager).GetNestedType("InitState", BindingFlags.NonPublic); if (null != tInitState) { fiStateValues = tInitState.GetFields(); } FieldInfo fiInit = typeof(System.Configuration.ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static); FieldInfo fiSystem = typeof(System.Configuration.ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static); if (fiInit != null && fiSystem != null && null != fiStateValues) { fiInit.SetValue(null, fiStateValues[1].GetValue(null)); fiSystem.SetValue(null, null); } ConfigSystem confSys = new ConfigSystem(); // Type configFactoryType = Type.GetType // ("System.Configuration.Internal.InternalConfigSettingsFactory, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", // true); Type configFactoryType = Type.GetType ("System.Configuration.Internal.InternalConfigSettingsFactory, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true); IInternalConfigSettingsFactory configSettingsFactory = (IInternalConfigSettingsFactory)Activator.CreateInstance(configFactoryType, true); configSettingsFactory.SetConfigurationSystem(confSys, false); // Type clientConfigSystemType = Type.GetType // ("System.Configuration.ClientConfigurationSystem, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true); // clientConfigSystem = (IInternalConfigSystem)Activator.CreateInstance(clientConfigSystemType, true); Type clientConfigSystemType = Type.GetType ("System.Configuration.ClientConfigurationSystem, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true); clientConfigSystem = (IInternalConfigSystem)Activator.CreateInstance(clientConfigSystemType, true); }
static void Main(string[] args) { using (Mutex m = new Mutex(false, "Global\\" + appGuid)) { //MessageBox.Show("Test 1.0.0.2"); SCUtility.SystemEventLog("Start BC System!!", EventLogEntryType.Information); //檢查是否同名Mutex已存在(表示另一份程式正在執行) //if (!m.WaitOne(0, false)) //{ // Console.WriteLine("Only one instance is allowed!"); // SCUtility.SystemEventLog("Can Not Execute Multiple BC System!!", EventLogEntryType.Warning); // var confirmResult = MessageBox.Show("Can Not Execute Multiple BC System!!", // "Confirm Exit BC System!!", MessageBoxButtons.OK, MessageBoxIcon.Warning); // return; //} //////檢查是否有重複開啟BC System //System.Diagnostics.Process crProcess = System.Diagnostics.Process.GetCurrentProcess(); //System.Diagnostics.Process[] myProcess = System.Diagnostics.Process.GetProcessesByName(crProcess.ProcessName); //if (myProcess.Length > 1) //{ // SCUtility.SystemEventLog("Can Not Execute Multiple BC System!!", EventLogEntryType.Warning); // var confirmResult = MessageBox.Show("Can Not Execute Multiple BC System!", // "Confirm Exit BC System!", MessageBoxButtons.OK, MessageBoxIcon.Warning); // return; //} string bcName = Environment.GetEnvironmentVariable("BCNAME"); //Console.WriteLine("BCNAME:{0}", bcName); string argStr = SCUtility.stringListToString(" ", args.ToList()); if (BCFUtility.isEmpty(argStr)) { bcName = "OHS100"; args = new string[] { bcName }; argStr = bcName; } else { bcName = args[0]; } copyConfig(bcName); ConfigSystem.Install(); //var wi = WindowsIdentity.GetCurrent(); //var wp = new WindowsPrincipal(wi); //bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator); //if (!runAsAdmin) //{ // Console.WriteLine("Try Change Run As Admin."); // MessageBox.Show("Try Change Run As Admin."); // var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase); // processInfo.UseShellExecute = true; // processInfo.Verb = "runas"; // processInfo.Arguments = argStr; // // Start the new process // try // { // Process.Start(processInfo); // } // catch (Exception) // { // Console.WriteLine("Sorry, can't to start " + // "this program with administrator rights!"); // MessageBox.Show("Sorry, can't to start " + // "this application with administrator rights!"); // } // return; //} AppDomain currentDomain = AppDomain.CurrentDomain; //A0.12 currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); //A0.12 Application.ThreadException += Application_ThreadException; Thread.CurrentThread.CurrentUICulture = Properties.Settings.Default.UICulture; //Localization.BuildMultiLanguageResources(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new BCMainForm(args[0], args[1])); SCUtility.SystemEventLog("Close BC System!!", EventLogEntryType.Information); Application.ExitThread(); //crProcess.Kill(); } }