C# 如何创建/插入WMI对象?

C# 如何创建/插入WMI对象?,c#,windows,wmi,C#,Windows,Wmi,我正在做一个用C#管理Windows电源计划的软件,通过ManagementObjet很容易获取电源计划并设置其设置。但是我想创建一个新的电源计划,换句话说,创建一个新的WMI对象,我不知道怎么做 有人知道如何创建它吗?在WMI中不能这样做。您可以使用Win32 API进行电源方案管理(如上所述),以创建计划,然后使用WMI对其进行监视/管理 要创建电源方案,您需要 首先通过以下方式复制现有计划: 使用PowerDuplicate方案 函数,指定 你希望你的新计划的基础 策划。你应该抄一本 内置

我正在做一个用C#管理Windows电源计划的软件,通过ManagementObjet很容易获取电源计划并设置其设置。但是我想创建一个新的电源计划,换句话说,创建一个新的WMI对象,我不知道怎么做


有人知道如何创建它吗?

在WMI中不能这样做。您可以使用Win32 API进行电源方案管理(如上所述),以创建计划,然后使用WMI对其进行监视/管理

要创建电源方案,您需要 首先通过以下方式复制现有计划: 使用PowerDuplicate方案 函数,指定 你希望你的新计划的基础 策划。你应该抄一本 内置方案,并修改 根据您的需要设置电源


在WMI中无法执行此操作。您可以使用Win32 API进行电源方案管理(如上所述),以创建计划,然后使用WMI对其进行监视/管理

要创建电源方案,您需要 首先通过以下方式复制现有计划: 使用PowerDuplicate方案 函数,指定 你希望你的新计划的基础 策划。你应该抄一本 内置方案,并修改 根据您的需要设置电源


现在它开始工作了。。。下面是我是如何做到的:

using System.Runtime.InteropServices;


[DllImport("powrprof.dll", EntryPoint = "PowerDuplicateScheme", SetLastError = true)]
        public static extern UInt32 PowerDuplicateScheme(IntPtr RootPowerKey, ref Guid SrcSchemeGuid, ref IntPtr DstSchemeGuid);


public static Guid createNewPowerPlan()
{
    Guid result = new Guid();
    IntPtr RetrPointer = IntPtr.Zero;

    // Attempt to duplicate the 'Balanced' Power Scheme.
    NativeMethods.PowerDuplicateScheme(IntPtr.Zero, ref VISA_PM_BASIC_SCHEMES.BALANCED, ref RetrPointer);

    if (RetrPointer != IntPtr.Zero)
    {
        // Function returns a pointer-to-memory, marshal back to our Guid variable.
        result = (Guid)Marshal.PtrToStructure(RetrPointer, typeof(Guid));
    }

    return result;
}

谢谢你的帮助

现在它开始工作了。。。下面是我是如何做到的:

using System.Runtime.InteropServices;


[DllImport("powrprof.dll", EntryPoint = "PowerDuplicateScheme", SetLastError = true)]
        public static extern UInt32 PowerDuplicateScheme(IntPtr RootPowerKey, ref Guid SrcSchemeGuid, ref IntPtr DstSchemeGuid);


public static Guid createNewPowerPlan()
{
    Guid result = new Guid();
    IntPtr RetrPointer = IntPtr.Zero;

    // Attempt to duplicate the 'Balanced' Power Scheme.
    NativeMethods.PowerDuplicateScheme(IntPtr.Zero, ref VISA_PM_BASIC_SCHEMES.BALANCED, ref RetrPointer);

    if (RetrPointer != IntPtr.Zero)
    {
        // Function returns a pointer-to-memory, marshal back to our Guid variable.
        result = (Guid)Marshal.PtrToStructure(RetrPointer, typeof(Guid));
    }

    return result;
}
谢谢你的帮助