C#.NET:如何检查我们是否使用电池?

问题描述:

我想成为一名优秀的开发人员公民,pay my taxes,如果我们在远程桌面上运行或使用电池运行,则禁用这些功能。C#.NET:如何检查我们是否使用电池?

如果我们正在运行远程桌面(或等同于终端服务器会话),我们必须禁用动画和双缓冲。你可以检查:

/// <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; 
    } 
} 

现在我需要找出用户是否在使用电池供电。如果他们是,我不想吹他们的电池。我想要做的事情,如

  • 禁用动画
  • 禁用后台拼写检查
  • 启动后台打印
  • 关闭梯度
  • 使用graphics.SmoothingMode = SmoothingMode.HighSpeed;
  • 使用graphics.InterpolationMode = InterpolationMode.Low;
  • 使用graphics.CompositingQuality = CompositingQuality.HighSpeed;
  • mi nimize硬盘接入 - 避免旋转了起来
  • 最小化网络访问 - 以节省电力的WiFi

有没有一种管理方法,如果机器是目前电池运行?

参考

How do you convince developers to pay their "taxes"?

Taxes: Remote Desktop Connection and painting

GetSystemMetrics(SM_REMOTESESSION)

我相信你可以检查SystemInformation.PowerStatus,看它是否对电池或没有。

Boolean isRunningOnBattery = 
     (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus == 
     PowerLineStatus.Offline); 

编辑:除上述之外,还有一个System.Windows.Forms。 PowerStatus班。其中一种方法是PowerLineStatus,如果它使用交流电,它将等于PowerLineStatus.Online。

+0

台式电脑在DC(例如UPS/IPS)上运行时,这与其相当甚么? – lbrahim 2015-11-08 10:56:24

你可以使用WMI(Windows管理规范)来查询操作系统对电池状态。

你可以在这里找到更多的信息:

希望有所帮助。

您可以使用使用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); 
     } 
    } 
} 
+0

只是为了获取信息,我有一个Windows平板电脑,不使用win32_Battery或PowerStatus返回信息。但是这工作。 +1。谢谢。 – user1531978 2016-02-20 22:33:24

我不相信它在托管代码中公开,但您可以通过pinvoke使用Win32 GetSystemPowerStatus来获取此信息。另外,您可能需要考虑使用GetCurrentPowerPolicies或类似方法来确定与性能/电源管理相关的用户首选项。

+0

快速浏览API(http://msdn.microsoft.com/en-us/library/aa372689(VS.85).aspx)并未显示与任何用户首选项相关的任何内容。它会返回CPU的运行速度,以及显示器将变暗并且风扇将关闭等情况。 – 2008-10-27 19:53:32

+0

是 - CPU速度是可以通过“电源设置”控制面板控制的用户首选项。 只是一个建议。 – 2008-10-27 21:27:58

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); 
    } 
}