Пример #1
0
        static void Main()
        {
            try
            {
                waitHandle = new AutoResetEvent(false);
                foreach (Process theProcess in Process.GetProcessesByName("EXCEL"))
                {
                    theProcess.Kill();
                }

                if (Process.GetProcessesByName("FAMS").Length > 1)
                {
                    foreach (Process theProcess in Process.GetProcessesByName("FAMS"))
                    {
                        theProcess.Kill();
                    }
                }

#if !DEBUG
                //if (!IsRunningAsAdministrator()) { Restart(); return; }
#endif

                using (Microsoft.Win32.RegistryKey rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\International", true))
                {
                    rkey.SetValue("sShortDate", "dd/MM/yyyy");
                    rkey.Close();
                }

                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
                Application.ApplicationExit += (ss, ee) =>
                {
                    foreach (Process theProcess in Process.GetProcessesByName("EXCEL"))
                    {
                        theProcess.Kill();
                    }

                    foreach (Process theProcess in Process.GetProcessesByName("FAMS"))
                    {
                        theProcess.Kill();
                    }
                };

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                /*GF.Settings("Reset();
                 * GF.Settings("Save();*/

                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("FAMS_Settings", true);
                if (key == null)
                {
                    key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("FAMS_Settings");
                    key.SetValue("host_url", "fams.fit-answer.com");
                    key.SetValue("branch_id", "");
                    key.SetValue("tmp_path", "C:\\FAMS\\TMP\\");
                    key.SetValue("member_prefix", "");
                    key.SetValue("localCardPath", "C:\\FAMS\\MEMBERCARD\\");
                    key.SetValue("card_printer", "");
                    key.SetValue("receipt_printer", "");
                    key.SetValue("vat", "");
                    key.SetValue("emp_card", "C:\\FAMS\\EMPCARD\\");
                }
                else
                {
                    if (key.GetValue("host_url") == null)
                    {
                        key.SetValue("host_url", "fams.fit-answer.com");
                    }
                    if (key.GetValue("branch_id") == null)
                    {
                        key.SetValue("branch_id", "");
                    }
                    if (key.GetValue("tmp_path") == null)
                    {
                        key.SetValue("tmp_path", "C:\\FAMS\\TMP\\");
                    }
                    if (key.GetValue("member_prefix") == null)
                    {
                        key.SetValue("member_prefix", "");
                    }
                    if (key.GetValue("localCardPath") == null)
                    {
                        key.SetValue("localCardPath", "C:\\FAMS\\MEMBERCARD\\");
                    }
                    if (key.GetValue("card_printer") == null)
                    {
                        key.SetValue("card_printer", "");
                    }
                    if (key.GetValue("receipt_printer") == null)
                    {
                        key.SetValue("receipt_printer", "");
                    }
                    if (key.GetValue("vat") == null)
                    {
                        key.SetValue("vat", "");
                    }
                    if (key.GetValue("emp_card") == null)
                    {
                        key.SetValue("emp_card", "C:\\FAMS\\EMPCARD\\");
                    }
                }

                //key.SetValue("host_url", "fams.topladyshop.net");
                key.SetValue("host_url", "fams.fit-answer.com");

                bool nullConfig = false;
                foreach (string valueName in key.GetValueNames())
                {
                    Console.WriteLine(valueName + " = " + key.GetValue(valueName));
                }

                foreach (string valueName in key.GetValueNames())
                {
                    if (string.IsNullOrEmpty((key.GetValue(valueName) ?? "").ToString()))
                    {
                        nullConfig = true;
                        break;
                    }
                }
                key.Close();
                key.Dispose();

                GF.cultureList = new List <string>();

                CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

                foreach (CultureInfo culture in cultures)
                {
                    var region = new RegionInfo(culture.LCID);

                    if (!(GF.cultureList.Contains(region.EnglishName)))
                    {
                        GF.cultureList.Add(region.EnglishName);
                    }
                }

                string tmp_path = GF.Settings("tmp_path");

                if (!Directory.Exists(tmp_path))
                {
                    Directory.CreateDirectory(tmp_path);
                }
                else
                {
                    var di = new DirectoryInfo(tmp_path);

                    foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
                    {
                        file.Attributes &= ~FileAttributes.ReadOnly;
                    }
                }

                var downloadedMessageInfo = new DirectoryInfo(tmp_path);

                foreach (FileInfo file in downloadedMessageInfo.GetFiles())
                {
                    file.Delete();
                }
                foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
                {
                    dir.Delete(true);
                }

                GF.CreateDirectoryWithEveryonePermission(tmp_path);

                Thread.CurrentThread.CurrentCulture   = new CultureInfo("en-GB");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");

                if (nullConfig)
                {
                    Console.WriteLine("!! NULL CONFIG !!");
                }
                else
                {
                    Console.WriteLine("^^ HAS CONFIG ^^");
                }

                Global.Capture.StartupCheck();

                if (DB.IsServerAlive())
                {
                    if (DB.IsDBAlive())
                    {
                        if (nullConfig)
                        {
                            Application.Run(new init_config());
                        }
                        else
                        {
                            Application.Run(new Login());
                        }
                    }
                }

                return;
            }
            catch (Win32Exception e)
            {
                GF.printError("********** APPLICATION EXIT WITH CODE (" + e.ErrorCode + ") **********");
                GF.printError(e.Message + "\r\n");

                GF.printError("FILE : " + new StackTrace(e, true).GetFrame(0).GetFileName());
                GF.printError("LINE : " + new StackTrace(e, true).GetFrame(0).GetFileLineNumber());

                GF.printError("STACK-TRACE : \r\n" + e.StackTrace.ToString() + "\r\n");

                if (currentProcess != null)
                {
                    currentProcess.Kill();
                }
                System.Environment.Exit(0);
            }
        }