❔ ✅ How do I import System.Windows.Devices.Power?

I know I need to make a namespace, but how? It doesn't show up on the list and I just want to aggravate battery (https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/get-battery-info)
Get battery information - UWP applications
Learn how to get detailed battery information using APIs in the Windows.Devices.Power namespace.
196 Replies
TurboCharged42
TurboCharged42OP2y ago
how do i add the namespace/refrence or whatever? (also please note i literally just started using visual studio and everything seems confusing)
TurboCharged42
TurboCharged42OP2y ago
Pobiega
Pobiega2y ago
Is your app a UWP app?
TurboCharged42
TurboCharged42OP2y ago
no
Pobiega
Pobiega2y ago
Those are UWP libraries, so I imagine they might not work outside UWP.
TurboCharged42
TurboCharged42OP2y ago
oh All I want to do is get detailed battery info
TurboCharged42
TurboCharged42OP2y ago
I would like to get at least some of these details (this came from aggrevate battery)
Pobiega
Pobiega2y ago
What details are you after that are not present in the PowerStatus class? there is something called the Win32_Battery module that you could access with some extern calls, but thats... non trivial
TurboCharged42
TurboCharged42OP2y ago
hmm how would that work oh thats way more than enough information for me but... how would those external calls work? do i Retrieving a WMI Class?
TurboCharged42
TurboCharged42OP2y ago
hmmm should I look at these examples?
TurboCharged42
TurboCharged42OP2y ago
TurboCharged42
TurboCharged42OP2y ago
do I Get-WmiObject -query "SELECT * FROM meta_class WHERE __class = 'Win32_LogicalDisk'" ummm do i replace SELECT, FROM and WHERE? oh wait that's not C# code...
Pobiega
Pobiega2y ago
nope, thats powershell but if you figure out the query, thats a start you can do WMI queries from C# just fine
TurboCharged42
TurboCharged42OP2y ago
Retrieving a WMI Class - Win32 apps
Provides a list of steps on how to retrieve a WMI class definition using Powershell, C#, VBScript, and C++.
TurboCharged42
TurboCharged42OP2y ago
oh
Pobiega
Pobiega2y ago
thats about getting the class definition thou, not the actual values. Might still be relevant
TurboCharged42
TurboCharged42OP2y ago
hm what's a class definition
Pobiega
Pobiega2y ago
So, im on a desktop computer atm so everything appears to be null for me so hard to test...
TurboCharged42
TurboCharged42OP2y ago
oh that's ok
Pobiega
Pobiega2y ago
using System.Management;

namespace ConsoleApp4;

public class Program
{
public static void Main(string[] args)
{
var query = new SelectQuery("SELECT * FROM Win32_Battery");
var searcher = new ManagementObjectSearcher(query);
var results = searcher.Get();

Console.WriteLine("Testing query...");
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}

Console.WriteLine("Testing ManagementClass...");
var manClass = new ManagementClass("Win32_Battery");

foreach (var property in manClass.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
using System.Management;

namespace ConsoleApp4;

public class Program
{
public static void Main(string[] args)
{
var query = new SelectQuery("SELECT * FROM Win32_Battery");
var searcher = new ManagementObjectSearcher(query);
var results = searcher.Get();

Console.WriteLine("Testing query...");
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}

Console.WriteLine("Testing ManagementClass...");
var manClass = new ManagementClass("Win32_Battery");

foreach (var property in manClass.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
try this see what it gives you the query doesnt seem to do anything, but the manclass stuff actually prints the properties, they just all have null values
TurboCharged42
TurboCharged42OP2y ago
wait do i need to import the namespaces
Pobiega
Pobiega2y ago
looks like you are using .NET framework? my code is from .NET 7
TurboCharged42
TurboCharged42OP2y ago
ok it improved
Pobiega
Pobiega2y ago
and yes, you need to install the nuget package for System.Management
TurboCharged42
TurboCharged42OP2y ago
I think somethings wrong
Pobiega
Pobiega2y ago
well, you cant just dump my code randomly into yours and expect it to work lol what I sent was a modern .NET console app code you dumped that into what appears to be a .NET Framework winforms project?
TurboCharged42
TurboCharged42OP2y ago
i kinda have no idea since i based my program off a tray thingy on github :/
Pobiega
Pobiega2y ago
well, find your main method and copy the content of my main method into yours make sure you comment out or otherwise save the current one thou this is just sample code to see what information we can get or, better yet, make a new console app and use my code
TurboCharged42
TurboCharged42OP2y ago
yay no errors no i need to run it Also thanks for all ur help so far!!! Severity Code Description Project File Line Suppression State Error CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. PowerTray C:\Users\myfakeusername\OneDrive\Desktop\Coding\PowerTray\Program.cs 12 Active hmm
Pobiega
Pobiega2y ago
pretty self explanatory: you can only have one main method you copied mine, so now you have two
TurboCharged42
TurboCharged42OP2y ago
how do i do that
Pobiega
Pobiega2y ago
It feels a bit like you are.. uh.. reaching above your current understanding?
TurboCharged42
TurboCharged42OP2y ago
yeah
Pobiega
Pobiega2y ago
Main methods are the very basic of programming
TurboCharged42
TurboCharged42OP2y ago
I know less C# than I thought i do
Pobiega
Pobiega2y ago
its your entrypoint, where the code starts to run
TurboCharged42
TurboCharged42OP2y ago
I mean im used to python yes
Pobiega
Pobiega2y ago
python is different, since each file is executable in theory but with C# its not
TurboCharged42
TurboCharged42OP2y ago
yes
Pobiega
Pobiega2y ago
show your solution tree you probably have a Program.cs file somewhere
TurboCharged42
TurboCharged42OP2y ago
oh yeah in fact i do
Pobiega
Pobiega2y ago
thats where your current main method will be
TurboCharged42
TurboCharged42OP2y ago
TurboCharged42
TurboCharged42OP2y ago
so i should move it here?
Pobiega
Pobiega2y ago
so comment that out all of it and put my code in there instead
TurboCharged42
TurboCharged42OP2y ago
what;s the kebind for comment? lol
Pobiega
Pobiega2y ago
eerrr Ctrl+K '
TurboCharged42
TurboCharged42OP2y ago
with the '?
Pobiega
Pobiega2y ago
yes
TurboCharged42
TurboCharged42OP2y ago
ohhh
Pobiega
Pobiega2y ago
but thats on my swedish layout :p
TurboCharged42
TurboCharged42OP2y ago
im used to just Ctrl K
Pobiega
Pobiega2y ago
no clue what you press ctrl+K is the "meta" key for enabling a ton of editor shortcuts in VS Ctrl+K, D is "format document" for example
TurboCharged42
TurboCharged42OP2y ago
oh its ctrl k ctrl c
TurboCharged42
TurboCharged42OP2y ago
Pobiega
Pobiega2y ago
remove that entire line not needed.
TurboCharged42
TurboCharged42OP2y ago
yay something happneed
TurboCharged42
TurboCharged42OP2y ago
all of them are null?
Pobiega
Pobiega2y ago
yup.
TurboCharged42
TurboCharged42OP2y ago
;_;
Pobiega
Pobiega2y ago
thats exactly what I got too
TurboCharged42
TurboCharged42OP2y ago
but why so close yet so far
Pobiega
Pobiega2y ago
dunno if you need to activate or refresh the values somehow who knows
TurboCharged42
TurboCharged42OP2y ago
well thanks for the code it seems useful maybe refresh every tick?
Pobiega
Pobiega2y ago
well, step 1 is to get the values populated somehow. All I find on google are 5-10 year old results about Win32_Battery dunno if its even used these days
TurboCharged42
TurboCharged42OP2y ago
welp
Pobiega
Pobiega2y ago
Some more googling tells me its up to the battery drivers to actually fill these fields with data and that sometimes they dont
TurboCharged42
TurboCharged42OP2y ago
hm well i guess its unrealiable? also are there breakpoints
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯
TurboCharged42
TurboCharged42OP2y ago
was this there before?
TurboCharged42
TurboCharged42OP2y ago
i thjink i may have gotten something
Pobiega
Pobiega2y ago
neat. did you change anything?
TurboCharged42
TurboCharged42OP2y ago
ahhh the debugger is soo confusing so,,, result is something but its not printing anything (I just moved it to a function that runs every tick or 1000ms or something I didn't write the code lol)
Pobiega
Pobiega2y ago
is that the result from the query, or from the management class?
TurboCharged42
TurboCharged42OP2y ago
the result from the management class i think its the one you tryed to print grr why isn't anything printing is it because i moved to a differebnt prgram? that gets run by the main one?
Pobiega
Pobiega2y ago
try this
Console.WriteLine("Testing ManagementClass...");
var manClass = new ManagementClass("Win32_Battery");

// foreach (var property in manClass.Properties)
// {
// Console.WriteLine($"{property.Name}: {property.Value}");
// }

Console.WriteLine("Testing instance...");
var instance = manClass.CreateInstance();

foreach (var property in instance.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
Console.WriteLine("Testing ManagementClass...");
var manClass = new ManagementClass("Win32_Battery");

// foreach (var property in manClass.Properties)
// {
// Console.WriteLine($"{property.Name}: {property.Value}");
// }

Console.WriteLine("Testing instance...");
var instance = manClass.CreateInstance();

foreach (var property in instance.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
maybe we need an instance of the management class it gave me the same results, but Im still on a desktop so
TurboCharged42
TurboCharged42OP2y ago
nope still nulls wait lemme try putting it in the tick function but for some reason its not printing anything if i put it in the other program
Pobiega
Pobiega2y ago
winform apps dont have console windows assigned
TurboCharged42
TurboCharged42OP2y ago
hmm
TurboCharged42
TurboCharged42OP2y ago
if all the main program does is run the other one, can i just combine them
TurboCharged42
TurboCharged42OP2y ago
idk why the original person decided to do that
Pobiega
Pobiega2y ago
yes wdym? thats how you start winforms apps.
TurboCharged42
TurboCharged42OP2y ago
wait really
TurboCharged42
TurboCharged42OP2y ago
Pobiega
Pobiega2y ago
what are you doing?
TurboCharged42
TurboCharged42OP2y ago
sooo im trying to rename program to program2 and make TrayIcon Program
Pobiega
Pobiega2y ago
no stop you are like an elephant in a china shop lol
TurboCharged42
TurboCharged42OP2y ago
is that a bad idea? uh oh should I click no
Pobiega
Pobiega2y ago
yes. then revert whatever change you just did the few lines of code I've sent so far you can put pretty much anywhere, but without a console window for them to print to, they wont do much
TurboCharged42
TurboCharged42OP2y ago
oh no
TurboCharged42
TurboCharged42OP2y ago
i need to clean up this
Pobiega
Pobiega2y ago
I really suggest you create a brand new console app for this it would be much easier to solve the battery issue separately and then later integrate it with your tray icon
TurboCharged42
TurboCharged42OP2y ago
so I shouldn;t combine the 2 programs then ok
Pobiega
Pobiega2y ago
Not to be harsh, but it seems you don't really have any idea on how to do that :p
TurboCharged42
TurboCharged42OP2y ago
well i tried anyways.. I got most of it moved but my lack of knowledge of C# is killing me The program '[4172] PowerTray.exe' has exited with code 0 (0x0).
TurboCharged42
TurboCharged42OP2y ago
TurboCharged42
TurboCharged42OP2y ago
does it just do the main functiuon and just kill the program if so, how do i fix it
Pobiega
Pobiega2y ago
Main is your entry point
TurboCharged42
TurboCharged42OP2y ago
yes
Pobiega
Pobiega2y ago
thats the ONLY code that gets run
TurboCharged42
TurboCharged42OP2y ago
oh
Pobiega
Pobiega2y ago
if main doesnt invoke other code, it does not happen
TurboCharged42
TurboCharged42OP2y ago
so i need to move everything under it?
Pobiega
Pobiega2y ago
no. Okay rewind a bit, are you following my suggestion of making a new project to figure out the battery stuff in? or are you still trying to hack it into your existing program?
TurboCharged42
TurboCharged42OP2y ago
uhhhhh the latter ;_;
Pobiega
Pobiega2y ago
please stop.
TurboCharged42
TurboCharged42OP2y ago
ok ill make a new one which .net
Pobiega
Pobiega2y ago
console, .net 6 or 7
TurboCharged42
TurboCharged42OP2y ago
what's happneing?
TurboCharged42
TurboCharged42OP2y ago
im trying ro add a refrence but its empty why isn't there an assemplies folder
Pobiega
Pobiega2y ago
close that window rightclick the project and click "manage nuget packages"
TurboCharged42
TurboCharged42OP2y ago
ok
TurboCharged42
TurboCharged42OP2y ago
this one?
Pobiega
Pobiega2y ago
dont think so?
Pobiega
Pobiega2y ago
System.Management 7.0.0
Provides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure. Commonly Used Types: System.Management.ManagementClass System.Management.ManagementObject System.Management.SelectQuery
TurboCharged42
TurboCharged42OP2y ago
ok i imported
TurboCharged42
TurboCharged42OP2y ago
Pobiega
Pobiega2y ago
thats fine
TurboCharged42
TurboCharged42OP2y ago
nvm it works but its all null
Pobiega
Pobiega2y ago
.NET is cross platform by default, its just warning you that this is windows specific
TurboCharged42
TurboCharged42OP2y ago
oh ok
TurboCharged42
TurboCharged42OP2y ago
the tick uses this
TurboCharged42
TurboCharged42OP2y ago
so yeah every second it updates
Pobiega
Pobiega2y ago
yeah.
TurboCharged42
TurboCharged42OP2y ago
how do i integrate that
Pobiega
Pobiega2y ago
how'd you think?
TurboCharged42
TurboCharged42OP2y ago
it doens't like timer do i need to import a library i mean do a using thing
Pobiega
Pobiega2y ago
you seem to be stuck in old .NET framework land my friend
TurboCharged42
TurboCharged42OP2y ago
? timer was removed?
Pobiega
Pobiega2y ago
TurboCharged42
TurboCharged42OP2y ago
the tray app used 4.8
Pobiega
Pobiega2y ago
There are.. 4 different variations 🙂
TurboCharged42
TurboCharged42OP2y ago
four??
Pobiega
Pobiega2y ago
yeah, which is from 2016-ish
TurboCharged42
TurboCharged42OP2y ago
:( how do i create a function the runs every second the fact that there are 4 variants disturbs me greatly
Pobiega
Pobiega2y ago
using System.Management;

namespace ConsoleApp4;

public class Program
{
public static async Task Main(string[] args)
{
var periodicTimer = new PeriodicTimer(TimeSpan.FromSeconds(5));

while (await periodicTimer.WaitForNextTickAsync())
{
CheckBattery();
}
}

private static void CheckBattery()
{
Console.WriteLine(DateTime.Now.Second);

var manClass = new ManagementClass("Win32_Battery");
foreach (var property in manClass.Properties)
{
if (property.Value is not null)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
var instance = manClass.CreateInstance();
foreach (var property in instance.Properties)
{
if (property.Value is not null)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
}
using System.Management;

namespace ConsoleApp4;

public class Program
{
public static async Task Main(string[] args)
{
var periodicTimer = new PeriodicTimer(TimeSpan.FromSeconds(5));

while (await periodicTimer.WaitForNextTickAsync())
{
CheckBattery();
}
}

private static void CheckBattery()
{
Console.WriteLine(DateTime.Now.Second);

var manClass = new ManagementClass("Win32_Battery");
foreach (var property in manClass.Properties)
{
if (property.Value is not null)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
var instance = manClass.CreateInstance();
foreach (var property in instance.Properties)
{
if (property.Value is not null)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
}
this seems to be the simplest variant if you ask me
TurboCharged42
TurboCharged42OP2y ago
oh thanks lemme just copy and paste that real quick.
TurboCharged42
TurboCharged42OP2y ago
Pobiega
Pobiega2y ago
thats the second value, to show that its not stalled
TurboCharged42
TurboCharged42OP2y ago
oh
Pobiega
Pobiega2y ago
it only prints the data from teh battery if it wasnt null (read the code)
TurboCharged42
TurboCharged42OP2y ago
oh I see well thats Weird sooo should we just combine them
Pobiega
Pobiega2y ago
Don't see how that would help the fact of the matter is there doesnt ever seem to be anything in the .Value of the property I just fired up a laptop to try there too, same result
TurboCharged42
TurboCharged42OP2y ago
I did something reallly stupid that will make every competent C# programmer to get triggered prepate for omega facepalm
TurboCharged42
TurboCharged42OP2y ago
TurboCharged42
TurboCharged42OP2y ago
but hey it works it just doens't print anything
Pobiega
Pobiega2y ago
All you did was moved the static main method into your powertray class.
TurboCharged42
TurboCharged42OP2y ago
but then I ran it itself again I think oh wiat it doesn't print it because im running it in a seperate thingy
TurboCharged42
TurboCharged42OP2y ago
wait what
TurboCharged42
TurboCharged42OP2y ago
it does print
Pobiega
Pobiega2y ago
but what is it printing?
TurboCharged42
TurboCharged42OP2y ago
wait still null? but it was working before wellll remember how I said it worked :Win32_Battery.DeviceID=" 1539CPT-COSL16C2PB1" thats the only value that's not null wsoooo
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯
TurboCharged42
TurboCharged42OP2y ago
yeah its broklen
Pobiega
Pobiega2y ago
we never got the console to output it thou only ever in the debug window, and only once(?) just for the hell of it... try this https://www.ks-soft.net/hostmon.eng/downpage.htm download the "WMI explorer" there
TurboCharged42
TurboCharged42OP2y ago
i did
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
looks like this I dont get any instances under Win32_Battery or CIM_Battery
TurboCharged42
TurboCharged42OP2y ago
oh
Pobiega
Pobiega2y ago
see if you do there is also Win32_PortableBattery
TurboCharged42
TurboCharged42OP2y ago
TurboCharged42
TurboCharged42OP2y ago
for Win32_Battery
Pobiega
Pobiega2y ago
show the entire window please lots of important info cropped out
TurboCharged42
TurboCharged42OP2y ago
Pobiega
Pobiega2y ago
okay, so CIM_Battery might be worth a try and we need to iterate the instances
TurboCharged42
TurboCharged42OP2y ago
also
TurboCharged42
TurboCharged42OP2y ago
what about this
Pobiega
Pobiega2y ago
seems to be for phones might work, dunno
TurboCharged42
TurboCharged42OP2y ago
oh nvm
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
but lets try and query CIM_Battery
TurboCharged42
TurboCharged42OP2y ago
still null
TurboCharged42
TurboCharged42OP2y ago
Pobiega
Pobiega2y ago
using System.Management;

namespace ConsoleApp4;

public class Program
{
public static void Main(string[] args)
{
TryQuery();

TryManagementClass();
}

private static void TryManagementClass()
{
var manClass = new ManagementClass("CIM_Battery");
foreach (var instance in manClass.GetInstances())
{
foreach (var property in instance.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}

foreach (var property in instance.SystemProperties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}

private static void TryQuery()
{
var query = new ManagementObjectSearcher("SELECT * FROM CIM_Battery");
var batteries = query.Get();
foreach (ManagementObject battery in batteries)
{
if (battery == null)
{
Console.WriteLine("Battery was null.");
continue;
}

foreach (var property in battery.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
}
using System.Management;

namespace ConsoleApp4;

public class Program
{
public static void Main(string[] args)
{
TryQuery();

TryManagementClass();
}

private static void TryManagementClass()
{
var manClass = new ManagementClass("CIM_Battery");
foreach (var instance in manClass.GetInstances())
{
foreach (var property in instance.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}

foreach (var property in instance.SystemProperties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}

private static void TryQuery()
{
var query = new ManagementObjectSearcher("SELECT * FROM CIM_Battery");
var batteries = query.Get();
foreach (ManagementObject battery in batteries)
{
if (battery == null)
{
Console.WriteLine("Battery was null.");
continue;
}

foreach (var property in battery.Properties)
{
Console.WriteLine($"{property.Name}: {property.Value}");
}
}
}
}
try this in the console app
TurboCharged42
TurboCharged42OP2y ago
hmm
TurboCharged42
TurboCharged42OP2y ago
i mean it has some information
Pobiega
Pobiega2y ago
there we go! something
TurboCharged42
TurboCharged42OP2y ago
yayy but some values don't make sense
Pobiega
Pobiega2y ago
why doesnt it make sense?
TurboCharged42
TurboCharged42OP2y ago
whats estimared chante renmaining is that time or
Pobiega
Pobiega2y ago
check the description
TurboCharged42
TurboCharged42OP2y ago
wait they have descriptions
Pobiega
Pobiega2y ago
EstimatedChargeRemaining Data type: uint16 Access type: Read-only Qualifiers: Units ("percent") Estimate of the percentage of full charge remaining. This property is inherited from CIM_Battery.
there we go. its a percentage
TurboCharged42
TurboCharged42OP2y ago
wait where dd you get the from
TurboCharged42
TurboCharged42OP2y ago
oh they're the same?
Pobiega
Pobiega2y ago
CIM_Battery class - Win32 apps
The CIM_Battery class represents the capabilities and management of the battery logical device. This class applies to batteries in laptop systems and other internal and external batteries.
Pobiega
Pobiega2y ago
you can use this one, if it feels better :p
TurboCharged42
TurboCharged42OP2y ago
thanks so much for your time and code!!!!
Pobiega
Pobiega2y ago
use /close
Accord
Accord2y ago
Closed!
TurboCharged42
TurboCharged42OP2y ago
wait how do i get the precise % charge
TurboCharged42
TurboCharged42OP2y ago
these two are very important but they never display anything even when my computer is charging
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server