C# .NET:如何检查我们是否使用电池运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/241142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# .NET: How to check if we're running on battery?
提问by Ian Boyd
i want to be a good developer citizen, pay my taxes, and disable things if we're running over Remote Desktop, or running on battery.
我想成为一名优秀的开发者公民,缴纳税款,并在我们通过远程桌面运行或使用电池运行时禁用某些功能。
If we're running over remote desktop (or equivalently in a Terminal server session), we must disable animations and double-buffering. You can check this with:
如果我们在远程桌面上运行(或等效地在终端服务器会话中),我们必须禁用动画和双缓冲。您可以通过以下方式检查:
/// <summary>
/// Indicates if we're running in a remote desktop session.
/// If we are, then you MUST disable animations and double buffering i.e. Pay your taxes!
///
/// </summary>
/// <returns></returns>
public static Boolean IsRemoteSession
{
//This is just a friendly wrapper around the built-in way
get
{
return System.Windows.Forms.SystemInformation.TerminalServerSession;
}
}
Now i need to find out if the user is running on battery power. If they are, i don't want to blow through their battery. i want to do things such as
现在我需要确定用户是否使用电池供电。如果他们是,我不想吹透他们的电池。我想做一些事情,比如
- disable animations
- disable background spell-checking
- disable background printing
- turn off gradients
- use
graphics.SmoothingMode = SmoothingMode.HighSpeed;
- use
graphics.InterpolationMode = InterpolationMode.Low;
- use
graphics.CompositingQuality = CompositingQuality.HighSpeed;
- minimize hard drive access - to avoid spin up
- minimize network access - to save WiFi power
- 禁用动画
- 禁用后台拼写检查
- 禁用后台打印
- 关闭渐变
- 用
graphics.SmoothingMode = SmoothingMode.HighSpeed;
- 用
graphics.InterpolationMode = InterpolationMode.Low;
- 用
graphics.CompositingQuality = CompositingQuality.HighSpeed;
- 尽量减少硬盘访问 - 避免旋转
- 尽量减少网络访问 - 以节省 WiFi 电量
Is there a managed way to see if the machine is currentlyrunning on battery?
有没有一种管理方式来查看机器当前是否正在使用电池?
Bonus Reading
奖励阅读
采纳答案by Powerlord
I believe you can check SystemInformation.PowerStatusto see if it's on battery or not.
我相信您可以检查SystemInformation.PowerStatus以查看它是否使用电池。
Boolean isRunningOnBattery =
(System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus ==
PowerLineStatus.Offline);
Edit: In addition to the above, there's also a System.Windows.Forms.PowerStatusclass. One of its methods is PowerLineStatus, which will equal PowerLineStatus.Online if it's on AC Power.
编辑:除了上述之外,还有一个 System.Windows.Forms。电源状态类。它的方法之一是PowerLineStatus,如果它使用交流电源,它将等于 PowerLineStatus.Online 。
回答by Igal Tabachnik
You could use WMI (Windows Management Instrumentation) to query the operating system about the battery status.
您可以使用 WMI(Windows Management Instrumentation)来查询操作系统的电池状态。
You could find more information here:
您可以在此处找到更多信息:
- http://69.10.233.10/KB/system/Wmi_Processor_infoWrapper.aspx
- http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr07/hey0409.mspx
- http://www.codeproject.com/KB/cs/EverythingInWmi03.aspx
- http://69.10.233.10/KB/system/Wmi_Processor_infoWrapper.aspx
- http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr07/hey0409.mspx
- http://www.codeproject.com/KB/cs/EverythingInWmi03.aspx
Hope that helps.
希望有帮助。
回答by driis
You could use the GetSystemPowerStatus function using P/Invoke. See: http://msdn.microsoft.com/en-gb/library/aa372693.aspx
您可以通过 P/Invoke 使用 GetSystemPowerStatus 函数。请参阅:http: //msdn.microsoft.com/en-gb/library/aa372693.aspx
Here's an example:
下面是一个例子:
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);
}
}
}
回答by Andrew Grant
I don't believe it's exposed in managed code, but you can use the Win32 GetSystemPowerStatus via pinvoke to get this info.
我不相信它在托管代码中公开,但是您可以通过 pinvoke 使用 Win32 GetSystemPowerStatus 来获取此信息。
As an aside, you may want to consider using the GetCurrentPowerPolicies or similar to determine the users preferences relating to performance/power management.
顺便说一句,您可能需要考虑使用 GetCurrentPowerPolicies 或类似方法来确定与性能/电源管理相关的用户首选项。
回答by Ian Boyd
R. Bemrose found the managed call. Here's some sample code:
R. Bemrose 找到了托管呼叫。这是一些示例代码:
/// <summary>
/// Indicates if we're running on battery power.
/// If we are, then disable CPU wasting things like animations, background operations, network, I/O, etc
/// </summary>
public static Boolean IsRunningOnBattery
{
get
{
PowerLineStatus pls = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus;
//Offline means running on battery
return (pls == PowerLineStatus.Offline);
}
}
回答by Byte11
Powerlord's answer doesn't seem to work, probably because it was answered in 2008.
Powerlord 的回答似乎不起作用,可能是因为它是在 2008 年回答的。
Here is a version that worked for me:
这是一个对我有用的版本:
Boolean x = (System.Windows.SystemParameters.PowerLineStatus == System.Windows.PowerLineStatus.Offline);