C#.NET:如何检查我们是否使用电池运行?
如果我们通过远程桌面运行或使用电池运行,我想成为一名优秀的开发者公民, 支付我的税 ,并禁用一些东西。
如果我们在远程桌面上运行(或等效于终端服务器会话),我们必须禁用动画和双缓冲。 您可以通过以下方式检查:
/// /// Indicates if we're running in a remote desktop session. /// If we are, then you MUST disable animations and double buffering ie Pay your taxes! /// /// /// public static Boolean IsRemoteSession { //This is just a friendly wrapper around the built-in way get { return System.Windows.Forms.SystemInformation.TerminalServerSession; } }
现在我需要找出用户是否使用电池供电。 如果他们是,我不想吹他们的电池。 我想做的事情如
是否有管理方式来查看机器当前是否使用电池运行?
参考
你如何说服开发商支付“税”?
税:远程桌面连接和绘画
GetSystemMetrics的(SM_REMOTESESSION)
我相信你可以查看SystemInformation.PowerStatus以查看它是否正在使用电池。
Boolean isRunningOnBattery = (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Offline);
编辑:除了上面,还有一个System.Windows.Forms。 PowerStatus类。 其中一种方法是PowerLineStatus ,如果它在交流电源上,它将等于PowerLineStatus.Online。
R. Bemrose找到了托管电话。 这是一些示例代码:
/// /// Indicates if we're running on battery power. /// If we are, then disable CPU wasting things like animations, background operations, network, I/O, etc /// public static Boolean IsRunningOnBattery { get { PowerLineStatus pls = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus; //Offline means running on battery return (pls == PowerLineStatus.Offline); } }
您可以使用P / Invoke使用GetSystemPowerStatus函数。 请参阅: http : //msdn.microsoft.com/en-gb/library/aa372693.aspx
这是一个例子:
using System; using System.Runtime.InteropServices; namespace PowerStateExample { [StructLayout(LayoutKind.Sequential)] public class PowerState { public ACLineStatus ACLineStatus; public BatteryFlag BatteryFlag; public Byte BatteryLifePercent; public Byte Reserved1; public Int32 BatteryLifeTime; public Int32 BatteryFullLifeTime; // direct instantation not intended, use GetPowerState. private PowerState() {} public static PowerState GetPowerState() { PowerState state = new PowerState(); if (GetSystemPowerStatusRef(state)) return state; throw new ApplicationException("Unable to get power state"); } [DllImport("Kernel32", EntryPoint = "GetSystemPowerStatus")] private static extern bool GetSystemPowerStatusRef(PowerState sps); } // Note: Underlying type of byte to match Win32 header public enum ACLineStatus : byte { Offline = 0, Online = 1, Unknown = 255 } public enum BatteryFlag : byte { High = 1, Low = 2, Critical = 4, Charging = 8, NoSystemBattery = 128, Unknown = 255 } // Program class with main entry point to display an example. class Program { static void Main(string[] args) { PowerState state = PowerState.GetPowerState(); Console.WriteLine("AC Line: {0}", state.ACLineStatus); Console.WriteLine("Battery: {0}", state.BatteryFlag); Console.WriteLine("Battery life %: {0}", state.BatteryLifePercent); } } }
您可以使用WMI(Windows Management Instrumentation)向操作系统查询电池状态。
您可以在此处找到更多信息:
希望有所帮助。
Powerlord的答案似乎没有用,可能是因为它在2008年得到了解答。
这是一个适合我的版本:
Boolean x = (System.Windows.SystemParameters.PowerLineStatus == System.Windows.PowerLineStatus.Offline);
我不相信它在托管代码中暴露,但您可以通过pinvoke使用Win32 GetSystemPowerStatus来获取此信息。
另外,您可能需要考虑使用GetCurrentPowerPolicies或类似方法来确定与性能/电源管理相关的用户首选项。
上述就是C#学习教程:C#.NET:如何检查我们是否使用电池运行?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1027108.html