gpt4 book ai didi

.net - 使用 C# 更改 Windows 服务凭据的最佳方法是什么

转载 作者:行者123 更新时间:2023-12-04 14:53:31 25 4
gpt4 key购买 nike

我需要使用 C# 更改现有 Windows 服务的凭据。我知道有两种不同的方法可以做到这一点。

  • 更改服务配置,见 ChangeServiceConfig on pinvoke.net
  • ManagementObject.InvokeMethod 使用 Change 作为方法名称。

  • 两者似乎都不是一种非常“友好”的方式,我想知道我是否错过了另一种更好的方式来做到这一点。

    最佳答案

    这是使用 System.Management 类的一种快速而肮脏的方法。

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

    namespace ServiceTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    string theServiceName = "My Windows Service";
    string objectPath = string.Format("Win32_Service.Name='{0}'", theServiceName);
    using (ManagementObject mngService = new ManagementObject(new ManagementPath(objectPath)))
    {
    object[] wmiParameters = new object[11];
    wmiParameters[6] = @"domain\username";
    wmiParameters[7] = "password";
    mngService.InvokeMethod("Change", wmiParameters);
    }
    }
    }
    }

    关于.net - 使用 C# 更改 Windows 服务凭据的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/125875/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com