コード例 #1
0
        private void CreateService(string binPath, string arguments)
        {
            WindowsIdentity.GetCurrent().IsElevated().Should().BeTrue("process was not running as admin. You cannot create a service without running as administrator.");
            var scArgs = string.Format("create {0} start=auto binpath=\"{1} {2}\"", ServiceName, binPath, arguments);
            var result = GeneralProcessRunner.Run(@"C:\Windows\system32\sc.exe", scArgs);

            if (result.ExitCode != 0)
            {
                throw new Exception(string.Format(@"Could not create service: C:\Windows\system32\sc.exe {0}{1}Standard output: {2}{1}Error output:{3}",
                                                  scArgs, Environment.NewLine, result.StandardOutput, result.ErrorOutput));
            }

            Log.DebugFormat(@"Creating service C:\Windows\system32\sc.exe {0}", scArgs);
            Log.DebugFormat(@"Creating service standard output: {0}", result.StandardOutput);
            Log.DebugFormat(@"Creating service error output: {0}", result.ErrorOutput);
        }
コード例 #2
0
        private void DeleteService()
        {
            var scArgs = string.Format("delete {0}", ServiceName);
            var result = GeneralProcessRunner.Run(@"C:\Windows\system32\sc.exe", scArgs);

            if (result.ExitCode != 0 && result.ExitCode != ServiceDoesNotExist)
            {
                throw new Exception(string.Format(@"Could not delete service: C:\Windows\system32\sc.exe {0}{1}Standard output: {2}{1}Error output:{3}{1}Exit code:{4}",
                                                  scArgs, Environment.NewLine, result.StandardOutput, result.ErrorOutput, result.ExitCode));
            }

            Log.DebugFormat(@"Deleting service C:\Windows\system32\sc.exe {0}", scArgs);
            Log.DebugFormat(@"Deleting service standard output: {0}", result.StandardOutput);
            Log.DebugFormat(@"Deleting service error output: {0}", result.ErrorOutput);
            Log.DebugFormat(@"Deleting service exit code: {0}", result.ErrorOutput);
        }