简体   繁体   English

在C#中获取Windows电源计划/方案(使用WinAPI)

[英]Get Windows power plans/schemes in C# (using WinAPI)

I'm interested in getting all power plans that you have in your computer using C#. 我有兴趣使用C#获取计算机中的所有电源计划。

I was thinking you might be able to use the API PowerEnumerate function in some way: 我以为你可能会以某种方式使用API PowerEnumerate函数:

DWORD WINAPI PowerEnumerate(
  _In_opt_   HKEY RootPowerKey,
  _In_opt_   const GUID *SchemeGuid,
  _In_opt_   const GUID *SubGroupOfPowerSettingsGuid,
  _In_       POWER_DATA_ACCESSOR AccessFlags,
  _In_       ULONG Index,
  _Out_opt_  UCHAR *Buffer,
  _Inout_    DWORD *BufferSize
);

But I have no idea on how to as I really don't know C. So.. How can I like, enumerate through all available power plans and create aa list of them. 但是我不知道怎么样,因为我真的不知道C.所以..我怎么样,列举所有可用的电力计划并创建一个列表。 I then want to be able to access each power plans GUID and their "user friendly name". 然后,我希望能够访问每个电源计划GUID及其“用户友好名称”。

So.. Perhaps if someone who is good at using the WinAPI from C# who would like to help, that would be great - or if someone has a better solution. 所以..也许如果有人擅长使用C#的WinAPI想要提供帮助,那就太好了 - 或者如果有人有更好的解决方案。 I've really tried to find a good answer to this but there doesn't seem to be any. 我真的试图找到一个很好的答案,但似乎没有。 I think this would help a lot of people. 我认为这会对很多人有所帮助。

Can anyone help with this? 有人能帮忙吗?

This should do it: 这应该这样做:

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections.Generic;

public class Program
{
    [DllImport("PowrProf.dll")]
    public static extern UInt32 PowerEnumerate(IntPtr RootPowerKey, IntPtr SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, UInt32 AcessFlags, UInt32 Index, ref Guid Buffer, ref UInt32 BufferSize);

    [DllImport("PowrProf.dll")]
    public static extern UInt32 PowerReadFriendlyName(IntPtr RootPowerKey, ref Guid SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, IntPtr Buffer, ref UInt32 BufferSize);

    public enum AccessFlags : uint
    {
        ACCESS_SCHEME = 16,
        ACCESS_SUBGROUP = 17,
        ACCESS_INDIVIDUAL_SETTING = 18
    }

    private static string ReadFriendlyName(Guid schemeGuid)
    {
        uint sizeName = 1024;
        IntPtr pSizeName = Marshal.AllocHGlobal((int)sizeName);

        string friendlyName;

        try
        {
            PowerReadFriendlyName(IntPtr.Zero, ref schemeGuid, IntPtr.Zero, IntPtr.Zero, pSizeName, ref sizeName);
            friendlyName = Marshal.PtrToStringUni(pSizeName);
        }
        finally
        {
            Marshal.FreeHGlobal(pSizeName);
        }

        return friendlyName;
    }

    public static IEnumerable<Guid> GetAll()
    {
        var schemeGuid = Guid.Empty;

        uint sizeSchemeGuid = (uint)Marshal.SizeOf(typeof(Guid));
        uint schemeIndex = 0;

        while (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)AccessFlags.ACCESS_SCHEME, schemeIndex, ref schemeGuid, ref sizeSchemeGuid) == 0)
        {
            yield return schemeGuid;
            schemeIndex++;
        }
    }

    public static void Main()
    {   
        var guidPlans = GetAll();

        foreach (Guid guidPlan in guidPlans)
        {
            Console.WriteLine(ReadFriendlyName(guidPlan));
        }
    }
}

You might have to run this program as administrator for security purposes. 出于安全考虑,您可能必须以管理员身份运行此程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM