❔ ✅ 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
how do i add the namespace/refrence or whatever?
(also please note i literally just started using visual studio and everything seems confusing)
Is your app a UWP app?
no
Those are UWP libraries, so I imagine they might not work outside UWP.
oh
All I want to do is get detailed battery info
but (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.powerstatus?redirectedfrom=MSDN&view=windowsdesktop-7.0) works, but it doesn't give much
PowerStatus Class (System.Windows.Forms)
Indicates current system power status information.
I would like to get at least some of these details (this came from aggrevate battery)
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
Win32_Battery class - Win32 apps
Represents a battery connected to the computer system.
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?
hmmm should I look at these examples?
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...nope, thats powershell
but if you figure out the query, thats a start
you can do WMI queries from C# just fine
I got it from here: https://learn.microsoft.com/en-us/windows/win32/wmisdk/retrieving-a-class
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++.
oh
thats about getting the class definition thou, not the actual values. Might still be relevant
hm
what's a class definition
So, im on a desktop computer atm so everything appears to be null for me
so hard to test...
oh
that's ok
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
wait do i need to import the namespaces
looks like you are using .NET framework?
my code is from .NET 7
ok it improved
and yes, you need to install the nuget package for
System.Management
I think somethings wrong
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?
i kinda have no idea since i based my program off a tray thingy on github :/
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
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
pretty self explanatory: you can only have one main method
you copied mine, so now you have two
how do i do that
It feels a bit like you are.. uh.. reaching above your current understanding?
yeah
Main methods are the very basic of programming
I know less C# than I thought i do
its your entrypoint, where the code starts to run
I mean im used to python
yes
python is different, since each file is executable in theory
but with C# its not
yes
show your solution tree
you probably have a
Program.cs
file somewhereoh yeah
in fact i do
thats where your current main method will be
so i should move it here?
so comment that out
all of it
and put my code in there instead
what;s the kebind for comment?
lol
eerrr
Ctrl+K '
with the '?
yes
ohhh
but thats on my swedish layout :p
im used to just Ctrl K
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
oh its ctrl k ctrl c
remove that
entire line
not needed.
yay something happneed
all of them are null?
yup.
;_;
thats exactly what I got too
dunno if you need to activate or refresh the values somehow
who knows
well thanks for the code it seems useful
maybe refresh every tick?
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
welp
Some more googling tells me its up to the battery drivers to actually fill these fields with data
and that sometimes they dont
hm
well i guess its unrealiable?
also are there breakpoints
¯\_(ツ)_/¯
was this there before?
i thjink i may have gotten something
neat. did you change anything?
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)
is that the result from the query, or from the management class?
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?
try this
maybe we need an instance of the management class
it gave me the same results, but Im still on a desktop so
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
winform apps dont have console windows assigned
hmm
if all the main program does is run the other one, can i just combine them
idk why the original person decided to do that
yes
wdym?
thats how you start winforms apps.
wait really
what are you doing?
sooo im trying to rename program to program2 and make TrayIcon Program
no stop
you are like an elephant in a china shop lol
is that a bad idea?
uh oh
should I click no
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
oh no
i need to clean up this
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
so I shouldn;t combine the 2 programs then
ok
Not to be harsh, but it seems you don't really have any idea on how to do that :p
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).
does it just do the main functiuon and just kill the program
if so, how do i fix it
Main is your entry point
yes
thats the ONLY code that gets run
oh
if main doesnt invoke other code, it does not happen
so i need to move everything under it?
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?
uhhhhh the latter ;_;
please stop.
ok ill make a new one
which .net
console, .net 6 or 7
what's happneing?
im trying ro add a refrence
but its empty
why isn't there an assemplies folder
close that window
rightclick the project and click "manage nuget packages"
ok
this one?
dont think so?
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
ok i imported
thats fine
nvm it works
but its all null
.NET is cross platform by default, its just warning you that this is windows specific
oh ok
the tick uses this
so yeah every second it updates
yeah.
how do i integrate that
how'd you think?
it doens't like timer
do i need to import a library i mean do a
using
thingyou seem to be stuck in old .NET framework land my friend
?
timer was removed?
Timers
Learn what .NET timers to use in a multithreaded environment.
the tray app used 4.8
There are.. 4 different variations 🙂
four??
yeah, which is from 2016-ish
:(
how do i create a function the runs every second
the fact that there are 4 variants disturbs me greatly
this seems to be the simplest variant if you ask me
oh thanks
lemme just copy and paste that real quick.
thats the second value, to show that its not stalled
oh
it only prints the data from teh battery if it wasnt null (read the code)
oh I see
well thats Weird
sooo
should we just combine them
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
I did something reallly stupid that will make every competent C# programmer to get triggered
prepate for omega facepalm
All you did was moved the static main method into your powertray class.
but then I ran it itself again
I think
oh wiat
it doesn't print it because im running it in a seperate thingy
wait what
it does print
but what is it printing?
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
¯\_(ツ)_/¯
yeah its broklen
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
i did
looks like this
I dont get any instances under Win32_Battery or CIM_Battery
oh
see if you do
there is also
Win32_PortableBattery
for
Win32_Battery
show the entire window please
lots of important info cropped out
okay, so CIM_Battery might be worth a try
and we need to iterate the instances
also
what about this
seems to be for phones
might work, dunno
oh
nvm
but lets try and query CIM_Battery
still null
try this in the console app
hmm
i mean
it has some information
there we go! something
yayy but some values don't make sense
why doesnt it make sense?
whats estimared chante renmaining
is that time or
check the description
wait they have descriptions
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
wait where dd you get the from
Win32_Battery class - Win32 apps
Represents a battery connected to the computer system.
oh they're the same?
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.
you can use this one, if it feels better
:p
thanks so much for your time and code!!!!
use
/close
Closed!
wait
how do i get the precise % charge
these two are very important but they never display anything even when my computer is charging
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.