Ejemplo n.º 1
0
        private void butInstall_Click(object sender, EventArgs e)
        {
            if (_serviceFileInfo == null)
            {
                MessageBox.Show("Select a valid service path");
                return;
            }
            string serviceName = textName.Text;

            if (serviceName.Length < 8 || serviceName.Substring(0, 8) != "OpenDent")
            {
                MessageBox.Show("Error.  Service name must begin with \"OpenDent\".");
                return;
            }
            if (ServicesHelper.HasService(serviceName, _serviceFileInfo))
            {
                MessageBox.Show("Error.  Either a service with this name is already installed or there is another service installed from this directory.");
                return;
            }
            if (_serviceFileInfo.Name == "OpenDentalEConnector.exe" || _serviceFileInfo.Name == "OpenDentalService.exe")
            {
                FormWebConfigSettings FormWCS = new FormWebConfigSettings(_serviceFileInfo);
                FormWCS.ShowDialog();
                if (FormWCS.DialogResult != DialogResult.OK)
                {
                    return;
                }
            }
            try {
                string standardOutput;
                int    exitCode;
                ServicesHelper.Install(serviceName, _serviceFileInfo, out standardOutput, out exitCode);
                if (exitCode != 0)
                {
                    MessageBox.Show("Error. Exit code: " + exitCode + "\r\n" + standardOutput.Trim());
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Unexpected error installing the service:\r\n" + ex.Message);
            }
            ServiceController service = null;

            try {
                service = ServicesHelper.GetServiceByServiceName(serviceName);
            }
            catch (Exception ex) {
                ex.DoNothing();
            }
            if (service != null)
            {
                HadServiceInstalled = true;              //We verified that the service was successfully installed
                //Try to grant access to "Everyone" so that the service can be stopped and started by all users.
                try {
                    ServicesHelper.SetSecurityDescriptorToAllowEveryoneToManageService(service);
                }
                catch (Exception ex) {
                    MessageBox.Show("The service was successfully installed but there was a problem updating the permissions for managing the service."
                                    + "\r\nThe service may have to be manually stopped and started via an administrative user."
                                    + "\r\nThis can be cumbersome when updating to newer versions of the software."
                                    + "\r\n\r\n" + ex.Message);
                }
            }
            RefreshFormData();
        }