Beispiel #1
0
        private void okButton_Click(object sender, EventArgs e)
        {
            var    nameIntalador = Path.Combine(Path.GetTempPath(), "SetUpdateUI.exe");
            var    key           = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\WinperUpdate");
            string statUI        = key.GetValue("statUI").ToString();

            key.Close();
            if (!statUI.Equals("updated") && System.IO.File.Exists(nameIntalador))
            {
                key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\WinperUpdate");
                key.SetValue("statUI", "updated");
                key.Close();
                if (Utils.isCentralizado)
                {
                    var argument = string.Format("/DIR=\"{0}\" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /NOCANCEL", Directory.GetCurrentDirectory());
                    SvcWPUI.Start(new string[] { nameIntalador, argument });
                }
                else
                {
                    Process.Start(nameIntalador, string.Format("/DIR=\"{0}\"", Directory.GetCurrentDirectory()));
                }
            }
            else
            {
                this.Close();
            }
        }
Beispiel #2
0
        private void FormPrincipal_Load(object sender, EventArgs e)
        {
            Utils.isCentralizado = File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "ProcessWPUI.exe"));
            try
            {
                if (SvcWPUI.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    SvcWPUI.Stop();
                }
            }
            catch (Exception) { }

            if (string.IsNullOrEmpty(Utils.GetSetting("server")) || string.IsNullOrEmpty(Utils.GetSetting("port")))
            {
                Utils.SetSetting("server", ConfigurationManager.AppSettings["server"]);
                Utils.SetSetting("port", int.Parse(ConfigurationManager.AppSettings["port"]));
                Utils.SetSetting("sql", bool.Parse(ConfigurationManager.AppSettings["sql"]));
                Utils.SetSetting("cftp", ConfigurationManager.AppSettings["cftp"]);
            }


            timerPing.Start();
            Utils.RegistrarLog("Load.log", "UI Iniciado");
            Utils.RegistrarLog("Load.log", "-----");
            ContextMenu1.MenuItems.Add("Configurar Ambiente y Licencia", new EventHandler(this.Ambiente_Click));
            ContextMenu1.MenuItems[0].Enabled = true;

            ContextMenu1.MenuItems.Add("-");
            ContextMenu1.MenuItems.Add("&Acerca de...", new EventHandler(this.AcercaDe_Click));
            ContextMenu1.MenuItems[1].DefaultItem = true;

            ContextMenu1.MenuItems.Add("&Configuración", new EventHandler(this.ConfigGen_Click));

            ContextMenu1.MenuItems.Add("-");
            ContextMenu1.MenuItems.Add("&Salir", new EventHandler(this.Salir_Click));

            notifyIcon2.ContextMenu = ContextMenu1;

            WindowState   = FormWindowState.Minimized;
            ShowInTaskbar = false;
            timer1.Stop();

            string nroLicencia = "";
            string ambientecfg = "";
            string perfil      = "";
            string inRun       = "No";
            string installDir  = Directory.GetCurrentDirectory();
            string statUI      = "updated";

            try
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\WinperUpdate");
                nroLicencia = key.GetValue("Licencia").ToString();
                ambientecfg = key.GetValue("Ambientes").ToString();
                inRun       = key.GetValue("InRun").ToString();
                perfil      = key.GetValue("Perfil").ToString();
                installDir  = key.GetValue("InstallDir").ToString();
                statUI      = key.GetValue("StatUI").ToString();
                key.Close();
            }
            catch (Exception)
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\WinperUpdate");
                key.SetValue("Licencia", nroLicencia);
                key.SetValue("Ambientes", ambientecfg);
                key.SetValue("InRun", inRun);
                key.SetValue("Perfil", perfil);
                key.SetValue("InstallDir", installDir);
                key.SetValue("StatUI", statUI);
                key.Close();
            }

            string regUI = Path.Combine(Path.GetTempPath(), "regUI.bat");
            string exe   = Path.Combine(installDir, "WinPerUpdateUI.exe");

            int intentos = 0;
            var keyRun   = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\WinperUpdate");

            while (intentos < 3 && inRun.Equals("No"))
            {
                try
                {
                    if (File.Exists(regUI))
                    {
                        File.Delete(regUI);
                    }
                    if (!File.Exists(regUI))
                    {
                        File.WriteAllLines(regUI, new string[] {
                            "@echo off",
                            "cd %windir%\\system32",
                            "REG ADD \"HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v  WinperUpdate /t REG_SZ /d \"" + exe + "\"  /f >> regUI.log"
                        });
                    }
                    var pas = Utils.ShowDialogInput(string.Format("Se procederá a configurar WinAct en el arranque de Windows.\nEscriba la clave para el usuario {0}", Environment.UserName), "Clave de Usuario", true);

                    if (string.IsNullOrEmpty(pas))
                    {
                        intentos++;
                        MessageBox.Show(string.Format("No ha escrito ninguna clave, vuelva a intentarlo (Intento {0}/3)", intentos), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        var sec = new System.Security.SecureString();
                        for (int i = 0; i < pas.Length; i++)
                        {
                            sec.AppendChar(pas.ElementAt(i));
                        }

                        Process.Start(regUI, Environment.UserName, sec, Environment.UserDomainName);
                        inRun = "Si";
                        keyRun.SetValue("InRun", inRun);
                        var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                        key.SetValue("WinperUpdate", exe);
                        key.Close();
                        MessageBox.Show("WinAct fue configurado al inicio de Windows correctamente", "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    intentos++;
                    MessageBox.Show(string.Format("{0} (Intento {1}/3)", ex.Message, intentos), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                if (intentos >= 3 && inRun.Equals("No"))
                {
                    MessageBox.Show("El numero de intentos sobrepaso el límite.\n\nWinAct no se pudo configurar al inicio de Windows. Esto NO afectará el funcionamiento de WinAct", "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            keyRun.Close();

            if (!string.IsNullOrEmpty(nroLicencia))
            {
                string server = Utils.GetSetting("server");
                string port   = Utils.GetSetting("port");

                try
                {
                    string json = Utils.StrSendMsg(server, int.Parse(port), "checklicencia#" + nroLicencia + "#");
                    cliente = JsonConvert.DeserializeObject <ClienteBo>(json);
                    if (cliente != null)
                    {
                        json = Utils.StrSendMsg(server, int.Parse(port), "ambientes#" + cliente.Id.ToString() + "#");

                        ContextMenu1.MenuItems.Clear();

                        MenuItem addDevice = new MenuItem("&Estado de la Versión");
                        addDevice.Enabled = true;
                        ambientes         = JsonConvert.DeserializeObject <List <AmbienteBo> >(json);
                        foreach (var ambiente in ambientes)
                        {
                            var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\WinperUpdate\" + ambiente.Nombre);
                            if (key != null)
                            {
                                string dirwp = key.GetValue("DirWinper") == null ? "" : key.GetValue("DirWinper").ToString();
                                if (Directory.Exists(dirwp))
                                {
                                    addDevice.MenuItems.Add(new MenuItem(ambiente.Nombre, new EventHandler(this.Restaurar_Click)));
                                }
                                key.Close();
                            }
                        }


                        /*Obtiene los modulos contratados del cliente con sus respectivos componentes*/
                        json = Utils.StrSendMsg(server, int.Parse(port), "modulos#" + cliente.Id + "#");
                        Utils.ModulosContratados = JsonConvert.DeserializeObject <List <ModuloBo> >(json);

                        ContextMenu1.MenuItems.Add(addDevice);

                        ContextMenu1.MenuItems.Add("Configurar Ambiente y Licencia", new EventHandler(this.Ambiente_Click));
                        ContextMenu1.MenuItems[1].Enabled = true;

                        ContextMenu1.MenuItems.Add("-");
                        ContextMenu1.MenuItems.Add("&Acerca de...", new EventHandler(this.AcercaDe_Click));
                        ContextMenu1.MenuItems[2].DefaultItem = true;

                        ContextMenu1.MenuItems.Add("&Configuración", new EventHandler(this.ConfigGen_Click));

                        ContextMenu1.MenuItems.Add("-");
                        ContextMenu1.MenuItems.Add("&Salir", new EventHandler(this.Salir_Click));

                        notifyIcon2.ContextMenu = ContextMenu1;
                        timer1.Start();
                        timerUI.Start();

                        TipoVentana = -1;
                        notifyIcon2.BalloonTipIcon  = ToolTipIcon.None;
                        notifyIcon2.BalloonTipTitle = "WinActUI";
                        notifyIcon2.BalloonTipText  = "Acá se encuentra WinAct!";
                        notifyIcon2.ShowBalloonTip(5000);
                    }
                }
                catch (Exception ex)
                {
                    if (ContextMenu1.MenuItems.Count > 0)
                    {
                        ContextMenu1.MenuItems[0].Enabled = false;
                        ContextMenu1.MenuItems[1].Enabled = false;
                    }
                    else
                    {
                        ContextMenu1.MenuItems.Add("Configurar Ambiente y Licencia", new EventHandler(this.Ambiente_Click));
                        ContextMenu1.MenuItems[0].Enabled = false;

                        ContextMenu1.MenuItems.Add("-");
                        ContextMenu1.MenuItems.Add("&Acerca de...", new EventHandler(this.AcercaDe_Click));
                        ContextMenu1.MenuItems[1].DefaultItem = true;

                        ContextMenu1.MenuItems.Add("&Configuración", new EventHandler(this.ConfigGen_Click));

                        ContextMenu1.MenuItems.Add("-");
                        ContextMenu1.MenuItems.Add("&Salir", new EventHandler(this.Salir_Click));
                    }
                    MessageBox.Show("WinAct no pudo iniciarce correctamente, puede revisar el log 'Load.log'");
                    Utils.RegistrarLog("Load.log", ex.ToString());
                }
            }
        }