Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I know that there is a library in .NET Framework called System.Windows.Forms that includes SystemInformation.PowerStatus but is there an equivalent to this in .NET Core? I don't want to have to change my project to .NET Framework to do it so I was curious if there is a way to do it in .NET Core

What I have tried:

performance - C# .NET: How to check if we're running on battery? - Stack Overflow[^]
Posted
Updated 29-Sep-20 0:49am
v2

Looking at the .NET Reference Source, the SystemInformation.PowerStatus class is just a wrapper around a call to the Win32 function GetSystemPowerStatus[^].

[DllImport("kernel32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
        [ResourceExposure(ResourceScope.None)]
        public static extern bool GetSystemPowerStatus([In, Out] ref NativeMethods.SYSTEM_POWER_STATUS systemPowerStatus);
 
Share this answer
 
Comments
Maciej Los 28-Sep-20 15:28pm    
Dave, OP is looking for .Net Core code. WinAPI is for Windows only. ;)
Dave Kreskowiak 28-Sep-20 16:17pm    
I'm busy as hell right now, so I just provided what the reference source was using while waiting for a deployment to finish. That should be a decent start for further research.
According to the documentation, the PowerStatus class is still available in .NET Core 3.0 or later:
PowerStatus Class (System.Windows.Forms) | Microsoft Docs[^]
PowerStatus - .NET API Catalog[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900