在C#中获取Windows电源计划/方案(使用WinAPI)
我对使用C#获取计算机中的所有电源计划感兴趣 我在想,您可能能够以某种方式使用API函数:在C#中获取Windows电源计划/方案(使用WinAPI),c#,c++,windows,power-management,C#,C++,Windows,Power Management,我对使用C#获取计算机中的所有电源计划感兴趣 我在想,您可能能够以某种方式使用API函数: DWORD WINAPI PowerEnumerate( _In_opt_ HKEY RootPowerKey, _In_opt_ const GUID *SchemeGuid, _In_opt_ const GUID *SubGroupOfPowerSettingsGuid, _In_ POWER_DATA_ACCESSOR AccessFlags, _In_
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
);
但是我不知道该怎么做,因为我真的不知道C.所以。。我怎样才能喜欢,列举所有可用的电源计划,并创建一个列表。然后,我希望能够访问每个电源计划GUID及其“用户友好名称”
所以。。如果有人擅长使用来自C#的WinAPI并愿意提供帮助,那就太好了——或者有人有更好的解决方案。我真的试图找到一个很好的答案,但似乎没有。我想这会帮助很多人
有人能帮忙吗?这应该可以:
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));
}
}
}
使用系统;
使用System.Linq;
运用系统反思;
使用System.Runtime.InteropServices;
使用System.Collections.Generic;
公共课程
{
[DllImport(“powrpof.dll”)]
公共静态外部UInt32 PowerEnumerate(IntPtr RootPowerKey、IntPtr SchemeGuid、IntPtr SubsubsofPowerSettingGuid、UInt32访问标志、UInt32索引、ref Guid缓冲区、ref UInt32缓冲区大小);
[DllImport(“powrpof.dll”)]
公共静态外部UInt32 PowerReadFriendlyName(IntPtr RootPowerKey,ref-Guid SchemeGuid,IntPtr subsubsof PowerSettingGuid,IntPtr PowerSettingGuid,IntPtr Buffer,ref-UInt32 BufferSize);
公共枚举访问标志:uint
{
接入方案=16,
访问_子组=17,
访问\个人\设置=18
}
私有静态字符串ReadFriendlyName(Guid schemeGuid)
{
uint sizeName=1024;
IntPtr pSizeName=Marshal.AllocHGlobal((int)sizeName);
字符串友好名称;
尝试
{
PowerReadFriendlyName(IntPtr.Zero,ref schemeGuid,IntPtr.Zero,IntPtr.Zero,pSizeName,ref sizeName);
friendlyName=Marshal.PtrToStringUni(pSizeName);
}
最后
{
弗里赫全球元帅(pSizeName);
}
返回friendlyName;
}
公共静态IEnumerable 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\u SCHEME,schemeIndex,ref schemeGuid,ref sizeSchemeGuid)=0)
{
收益返回模式uid;
schemeIndex++;
}
}
公共静态void Main()
{
var guidPlans=GetAll();
foreach(guidPlan中的Guid guidPlan)
{
Console.WriteLine(ReadFriendlyName(guidPlan));
}
}
}
出于安全考虑,您可能必须以管理员身份运行此程序。就像我所希望的那样工作!非常感谢!!如果你有时间,如果你能链接到一些教程,我可以自己学习如何做这类事情,那就太好了。再次感谢!