Esempio n. 1
0
        public static System.ServiceProcess.ServiceStartMode getServiceStartUpType()
        {
            System.ServiceProcess.ServiceStartMode retval = System.ServiceProcess.ServiceStartMode.Manual;

            string value = ConfigurationManager.AppSettings["ServiceStartup"];

            if (!string.IsNullOrEmpty(value))
            {
                switch (value.ToLower().Trim())
                {
                case "automatic":
                    retval = System.ServiceProcess.ServiceStartMode.Automatic;
                    break;

                case "disabled":
                    retval = System.ServiceProcess.ServiceStartMode.Disabled;
                    break;

                case "manual":
                    retval = System.ServiceProcess.ServiceStartMode.Manual;
                    break;
                }
            }

            return(retval);
        }
        public void Install(String ServiceName, String DisplayName, String Description,
                            System.ServiceProcess.ServiceAccount Account,
                            System.ServiceProcess.ServiceStartMode StartMode)
        {
            System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            ProcessInstaller.Account = Account;

            System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

            System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
            string processPath = Process.GetCurrentProcess().MainModule.FileName;

            if (processPath != null && processPath.Length > 0)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(processPath);
                //Context = new System.Configuration.Install.InstallContext();
                //Context.Parameters.Add("assemblyPath", fi.FullName);
                //Context.Parameters.Add("startParameters", "Test");

                String   path    = String.Format("/assemblypath={0}", fi.FullName);
                String[] cmdline = { path };
                Context = new System.Configuration.Install.InstallContext("", cmdline);
            }

            SINST.Context     = Context;
            SINST.DisplayName = DisplayName;
            SINST.Description = Description;
            SINST.ServiceName = ServiceName;
            SINST.StartType   = StartMode;
            SINST.Parent      = ProcessInstaller;

            // http://bytes.com/forum/thread527221.html
            //            SINST.ServicesDependedOn = new String[] {};

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            SINST.Install(state);

            // http://www.dotnet247.com/247reference/msgs/43/219565.aspx
            using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Services\{0}", SINST.ServiceName), true))
            {
                try
                {
                    Object sValue = oKey.GetValue("ImagePath");
                    oKey.SetValue("ImagePath", sValue);
                }
                catch (Exception Ex)
                {
                    //                    System.Console.WriteLine(Ex.Message);
                }
            }
        }
Esempio n. 3
0
        public static void Install(String ServiceName, String DisplayName, String Description, String exeName,
                                   System.ServiceProcess.ServiceAccount Account,
                                   System.ServiceProcess.ServiceStartMode StartMode)
        {
            //http://www.theblacksparrow.com/
            System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            ProcessInstaller.Account = Account;

            System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

            System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
            System.IO.FileInfo fi = new System.IO.FileInfo(exeName);

            String path = String.Format("/assemblypath={0}", fi.FullName);

            String[] cmdline = { path };
            Context = new System.Configuration.Install.InstallContext("", cmdline);

            SINST.Context     = Context;
            SINST.DisplayName = DisplayName;
            SINST.Description = Description;
            SINST.ServiceName = ServiceName;
            SINST.StartType   = StartMode;
            SINST.Parent      = ProcessInstaller;

            // http://bytes.com/forum/thread527221.html
            //SINST.ServicesDependedOn = new String[] { "Spooler", "Netlogon", "Netman" };
            SINST.ServicesDependedOn = new String[] { };

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            SINST.Install(state);

            // http://www.dotnet247.com/247reference/msgs/43/219565.aspx
            using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Services\{0}", ServiceName), true))
            {
                try
                {
                    Object sValue = oKey.GetValue("ImagePath");
                    oKey.SetValue("ImagePath", sValue);
                }
                catch (Exception Ex)
                {
                    System.Windows.Forms.MessageBox.Show(Ex.Message);
                }
            }
        }
Esempio n. 4
0
        public void Install(string ServiceName, string DisplayName, string Description, System.ServiceProcess.ServiceAccount Account, System.ServiceProcess.ServiceStartMode StartMode)
        {
            System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            ProcessInstaller.Account = Account;

            System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

            System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
            string processPath = Process.GetCurrentProcess().MainModule.FileName;

            if (processPath != null && processPath.Length > 0)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(processPath);

                string   path    = string.Format("/assemblypath={0}", fi.FullName);
                string[] cmdline = { path };
                Context = new System.Configuration.Install.InstallContext("", cmdline);
            }

            SINST.Context            = Context;
            SINST.DisplayName        = DisplayName;
            SINST.Description        = Description;
            SINST.ServiceName        = ServiceName;
            SINST.StartType          = StartMode;
            SINST.Parent             = ProcessInstaller;
            SINST.ServicesDependedOn = new string[] { "RpcSs" };

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            SINST.Install(state);

            using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(string.Format(@"SYSTEM\CurrentControlSet\Services\{0}", ServiceName), true))
            {
                try
                {
                    Object sValue = oKey.GetValue("ImagePath");
                    oKey.SetValue("ImagePath", sValue);
                }
                catch (Exception Ex)
                {
                    Trace.TraceError(Ex.Message);
                }
            }
        }