Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
D

DafeNilesh

@DafeNilesh
About
Posts
1
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Window Service Startup Mode
    D DafeNilesh

    The Service startup mode can be changed in C# using System.Management namespace. a sample method is given below.

    /// <summary>
    /// This routine updates the start mode of the provided service.
    /// Add Reference to System.Management .net Assembly
    /// </summary>
    /// <param name="serviceName">Name of the service to be updated</param>
    /// <param name="startMode">Manual or Automatic. This parameter could probably use
    /// an enum.</param>
    /// <param name="errorMsg">If applicable, error message assoicated with exception</param>
    /// <returns>Success or failure. False is returned if service is not found.</returns>
    public bool ServiceStartModeUpdate(string serviceName, string startMode, out string errorMsg)
    {
    uint success = 1;
    errorMsg = string.Empty;

            string filter =
                String.Format("SELECT \* FROM Win32\_Service WHERE Name = '{0}'", serviceName);
    
            System.Management.ManagementObjectSearcher query = new ManagementObjectSearcher(filter);
    
            // No match = failed condition
            if (query == null) return false;
    
            try
            {
                ManagementObjectCollection services = query.Get();
    
                foreach (ManagementObject service in services)
                {
                    ManagementBaseObject inParams =
                        service.GetMethodParameters("ChangeStartMode");
                    inParams\["startmode"\] = startMode;
    
                    ManagementBaseObject outParams =
                        service.InvokeMethod("ChangeStartMode", inParams, null);
                    success = Convert.ToUInt16(outParams.Properties\["ReturnValue"\].Value);
                    //custom method
                    errorMsg = GetErrorMsg(success) + "\\n Try \\"Run as Administrator\\" ";
                }
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                throw;
            }
    
            return (success == 0);
        }
    
    C# help csharp database sql-server sysadmin
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups