コード例 #1
0
ファイル: FormServiceManage.cs プロジェクト: mnisl/OD
		private void butInstall_Click(object sender,EventArgs e) {
			if(_serviceFile==null) {
				MessageBox.Show("Select a valid service path");
				return;
			}
			if(textName.Text.Length<8 || textName.Text.Substring(0,8)!="OpenDent") {
				MessageBox.Show("Error.  Service name must begin with \"OpenDent\".");
				return;
			}
			List<ServiceController> allOpenDentServices=GetAllOpenDentServices();
			for(int i=0;i<allOpenDentServices.Count;i++) { //create list of all OpenDent service install paths to ensure only one service can be installed from each directory				
				if(textName.Text==allOpenDentServices[i].ServiceName) {
					MessageBox.Show("Error.  A service with this name is already installed.  Names must be unique.");
					return;
				}
				RegistryKey hklm=Registry.LocalMachine;
				hklm=hklm.OpenSubKey(@"System\CurrentControlSet\Services\"+allOpenDentServices[i].ServiceName);
				string installedServicePath=hklm.GetValue("ImagePath").ToString().Replace("\"","");
				if(installedServicePath==_serviceFile.FullName) {
					MessageBox.Show("Error.  Cannot install service.  This service is already installed from this directory.");
					return;
				}
			}
			if(_serviceFile.Name=="OpenDentalCustListener.exe") {
				FormWebConfigSettings FormWCS=new FormWebConfigSettings(_serviceFile);
				FormWCS.ShowDialog();
				if(FormWCS.DialogResult!=DialogResult.OK) {
					return;
				}
			}
			Process process=new Process();
			process.StartInfo.WorkingDirectory=_serviceFile.DirectoryName;
			process.StartInfo.FileName=Path.Combine(Directory.GetCurrentDirectory(),"installutil.exe");
			//new strategy for having control over servicename
			//InstallUtil /ServiceName=OpenDentHL7_abc OpenDentHL7.exe
			process.StartInfo.Arguments="/ServiceName="+textName.Text+" "+_serviceFile.Name;
			process.Start();
			try {
				process.WaitForExit(10000);
				if(process.ExitCode!=0) {
					MessageBox.Show("Error. Exit code:"+process.ExitCode.ToString());
				}
			}
			catch {
				MessageBox.Show("Error. Did not exit after 10 seconds.");
			}			
			butRefresh_Click(this,e);
		}
コード例 #2
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();
        }