❔ ✅ 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
how do i add the namespace/refrence or whatever? (also please note i literally just started using visual studio and everything seems confusing)
Pobiega
Pobiega2y ago
Is your app a UWP app?
TurboCharged42
no
Pobiega
Pobiega2y ago
Those are UWP libraries, so I imagine they might not work outside UWP.
TurboCharged42
oh All I want to do is get detailed battery info
TurboCharged42
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
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
hmmm should I look at these examples?
TurboCharged42
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
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
oh
Pobiega
Pobiega2y ago
thats about getting the class definition thou, not the actual values. Might still be relevant
TurboCharged42
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
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
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
ok it improved
Pobiega
Pobiega2y ago
and yes, you need to install the nuget package for System.Management
TurboCharged42
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
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
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
how do i do that
Pobiega
Pobiega2y ago
It feels a bit like you are.. uh.. reaching above your current understanding?
TurboCharged42
yeah
Pobiega
Pobiega2y ago
Main methods are the very basic of programming
TurboCharged42
I know less C# than I thought i do
Pobiega
Pobiega2y ago
its your entrypoint, where the code starts to run
TurboCharged42
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
yes
Pobiega
Pobiega2y ago
show your solution tree you probably have a Program.cs file somewhere
TurboCharged42
oh yeah in fact i do
Pobiega
Pobiega2y ago
thats where your current main method will be
TurboCharged42
so i should move it here?
Pobiega
Pobiega2y ago
so comment that out all of it and put my code in there instead
TurboCharged42
what;s the kebind for comment? lol
Pobiega
Pobiega2y ago
eerrr Ctrl+K '
TurboCharged42
with the '?
Pobiega
Pobiega2y ago
yes
TurboCharged42
ohhh
Pobiega
Pobiega2y ago
but thats on my swedish layout :p
TurboCharged42
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
oh its ctrl k ctrl c
Pobiega
Pobiega2y ago
remove that entire line not needed.
TurboCharged42
yay something happneed
TurboCharged42
all of them are null?
Pobiega
Pobiega2y ago
yup.
TurboCharged42
;_;
Pobiega
Pobiega2y ago
thats exactly what I got too
TurboCharged42
but why so close yet so far
Pobiega
Pobiega2y ago
dunno if you need to activate or refresh the values somehow who knows
TurboCharged42
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
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
hm well i guess its unrealiable? also are there breakpoints
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯
TurboCharged42
was this there before?
TurboCharged42
i thjink i may have gotten something
Pobiega
Pobiega2y ago
neat. did you change anything?
TurboCharged42
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
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
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
hmm
TurboCharged42
if all the main program does is run the other one, can i just combine them
TurboCharged42
idk why the original person decided to do that
Pobiega
Pobiega2y ago
yes wdym? thats how you start winforms apps.
TurboCharged42
wait really
Pobiega
Pobiega2y ago
what are you doing?
TurboCharged42
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
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
oh no
TurboCharged42
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
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
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
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
yes
Pobiega
Pobiega2y ago
thats the ONLY code that gets run
TurboCharged42
oh
Pobiega
Pobiega2y ago
if main doesnt invoke other code, it does not happen
TurboCharged42
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
uhhhhh the latter ;_;
Pobiega
Pobiega2y ago
please stop.
TurboCharged42
ok ill make a new one which .net
Pobiega
Pobiega2y ago
console, .net 6 or 7
TurboCharged42
what's happneing?
TurboCharged42
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
ok
TurboCharged42
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
ok i imported
Pobiega
Pobiega2y ago
thats fine
TurboCharged42
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
oh ok
TurboCharged42
the tick uses this
TurboCharged42
so yeah every second it updates
Pobiega
Pobiega2y ago
yeah.
TurboCharged42
how do i integrate that
Pobiega
Pobiega2y ago
how'd you think?
TurboCharged42
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
? timer was removed?
Pobiega
Pobiega2y ago
TurboCharged42
the tray app used 4.8
Pobiega
Pobiega2y ago
There are.. 4 different variations 🙂
TurboCharged42
four??
Pobiega
Pobiega2y ago
yeah, which is from 2016-ish
TurboCharged42
:( 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
oh thanks lemme just copy and paste that real quick.
Pobiega
Pobiega2y ago
thats the second value, to show that its not stalled
TurboCharged42
oh
Pobiega
Pobiega2y ago
it only prints the data from teh battery if it wasnt null (read the code)
TurboCharged42
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
I did something reallly stupid that will make every competent C# programmer to get triggered prepate for omega facepalm
TurboCharged42
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
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
wait what
TurboCharged42
it does print
Pobiega
Pobiega2y ago
but what is it printing?
TurboCharged42
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
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
i did
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
looks like this I dont get any instances under Win32_Battery or CIM_Battery
TurboCharged42
oh
Pobiega
Pobiega2y ago
see if you do there is also Win32_PortableBattery
TurboCharged42
for Win32_Battery
Pobiega
Pobiega2y ago
show the entire window please lots of important info cropped out
Pobiega
Pobiega2y ago
okay, so CIM_Battery might be worth a try and we need to iterate the instances
TurboCharged42
also
TurboCharged42
what about this
Pobiega
Pobiega2y ago
seems to be for phones might work, dunno
TurboCharged42
oh nvm
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
but lets try and query CIM_Battery
TurboCharged42
still null
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
hmm
TurboCharged42
i mean it has some information
Pobiega
Pobiega2y ago
there we go! something
TurboCharged42
yayy but some values don't make sense
Pobiega
Pobiega2y ago
why doesnt it make sense?
TurboCharged42
whats estimared chante renmaining is that time or
Pobiega
Pobiega2y ago
check the description
TurboCharged42
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
wait where dd you get the from
TurboCharged42
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
thanks so much for your time and code!!!!
Pobiega
Pobiega2y ago
use /close
Accord
Accord2y ago
Closed!
TurboCharged42
wait how do i get the precise % charge
TurboCharged42
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
More Posts
❔ Capture parameter value with Moq decorated with the 'in' keywordHello! I'm writing Unit Tests for a library with a lot of structs, which utilizes the `in` keyword t✅ Mention user in footer in Embed (.Net 6)Hi, I'm trying to mention the user to used the command in the footer but I can only make it write itMap complex object (dictionary) from appsettings.json to modelHi, i have a complex appsettings.json like this ``` "interceptor": { "interactionType": "redire❔ Passing a list of values to Stored Procedure in EF CoreHey devs, I would like to know whether it is possible to pass a list of values as a parameter to a ✅ Input into list (Console app)**Program** I want the user to keep creating accounts, and then put them inside at list **Example o❔ POST request in swagger shows entire schema and parts of it are not needed in request bodyI have a one to one relationship between Product and CartItem, in the POST request for Product the C❔ ❔ Docker-Compose cannot find DockerfileHey! when i try to do docker-compose build i get this error : `failed to solve: rpc error: code = U❔ IStringLocalizer with a GenericClass T in Resource filenameI'm using `Microsoft.Extensions.Localization.IStringLocalizer` to localize my strings via Resources ✅ Difference between [DataType(DataType.EmailAddress)] and [EmailAddress] data annotations?Can anyone let me know what's the difference between [DataType(DataType.EmailAddress)] and [EmailAdd✅ MS webshop exampleHello, Im trying to understand the different parts of a webshop example from Microsoft. I have a