怎么利用c#修改services的Startup type

所属分类: 软件编程 / C#教程 阅读数: 130
收藏 0 赞 0 分享

我们知道大部分的services的操作可以通过ServiceController来实现,包括services的开启,停止,暂停,还有获取service的status。但是这里关于services的修改Startup type这点,貌似ServiceController不好做到,我们可以这样来做:

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace ServicesStartup
{
    class Program
    {
        public enum StartupType
        {
            Automatic,
            Disabled,
            Manual
        }

        public static void SetStartupType(string serviceName, StartupType startupType)
        {
            string type = startupType.ToString();
            try
            {
                ManagementPath mp = new ManagementPath(string.Format("Win32_Service.Name='{0}'", serviceName));
                if (mp != null)
                {
                    using (ManagementObject mo = new ManagementObject(mp))
                    {
                        object[] parameters = new object[1] { type };
                        mo.InvokeMethod("ChangeStartMode", parameters);                       
                    }
                }
            }
            catch (ManagementException ex)
            {
                Console.WriteLine("An error occured while trying to searching the WMI method: " + ex.ToString());
            }

        }

        static void Main(string[] args)
        {
            SetStartupType("gupdate", StartupType.Automatic); 
            Console.ReadKey();
        }
    }
}


上面使用了ManagementPath类,或者你也可以这样:
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace ServicesStartup
{
    class Program
    {
       static void Main(string[] args)
        {
            try
            {
                ManagementObject classInstance = new ManagementObject("root\\CIMV2",
                    "Win32_Service.Name='gupdate'", null);

                // Obtain in-parameters for the method.
                ManagementBaseObject inParams = classInstance.GetMethodParameters("ChangeStartMode");
                // Add the input parameters.
                inParams["StartMode"] = "Automatic";               

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams = classInstance.InvokeMethod("ChangeStartMode", inParams, null);

                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch (ManagementException err)
            {
                Console.WriteLine("An error occured while trying to execute the WMI emthod: " + err.ToString());
            }
            Console.ReadKey();
        }
    }
}


这段代码使用的是ManagementObject类,里面输出的ReturnValue是一个标志,如果值为0就是修改成功了。

这里需要注意的一点:C#必须以管理员的权限运行才能达到效果的,不然service的startmode修改是没有效果的。

更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多