示例#1
0
        static int Main(string[] args)
        {
            string cfgPath = "service-policy.cfg";

            if (args.Length > 0)
                cfgPath = args[0];

            using (var cfg = new StreamReader(cfgPath))
            {
                while (!cfg.EndOfStream)
                {
                    var line = cfg.ReadLine();

                    if (line.Trim().StartsWith("#"))
                        continue;

                    var tokens = line.Split(':');

                    if (tokens.Length < 2)
                        continue;

                    string serviceName = tokens[0].Trim();
                    tokens[1] = tokens[1].Trim();
                    // Capitalize the first letter to make sure it matches enum members.
                    var mode = new string(tokens[1].ToCharArray().Select((c, index) => index == 0 ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c)).ToArray());

                    try
                    {
                        ServiceStartMode startMode = (ServiceStartMode)Enum.Parse(typeof(ServiceStartMode), mode);
                        ServiceController service = new ServiceController(serviceName);
                        service.SetStartMode(startMode);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Exception: {0}\n{1}", e.Message, e.StackTrace);
                        continue;
                    }
                }
            }
            return 0;
        }