Untitled

 avatar
unknown
plain_text
a year ago
2.2 kB
10
Indexable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

namespace PowerStats
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Pil bilgilerini alma işlemi
            try
            {
                ManagementObjectCollection pilBilgileri = WMIQuery("SELECT * FROM Win32_Battery");

                if (pilBilgileri.Count > 0)
                {
                    // İlk pili al
                    ManagementObject pil = pilBilgileri.Cast<ManagementObject>().FirstOrDefault();

                    if (pil != null)
                    {
                        // Pil kapasitesini ve kalan pil şarjını al
                        int kapasite = Convert.ToInt32(pil["DesignCapacity"]);
                        int kalanSarj = Convert.ToInt32(pil["FullChargeCapacity"]);

                        // Pil sağlığını hesapla
                        double pilSagligi = (double)kalanSarj / kapasite * 100;

                        // Pil bilgilerini formda görüntüle
                        label1.Text = kapasite.ToString() + " mAh";
                        label2.Text = kalanSarj.ToString() + " mAh";
                        label3.Text = pilSagligi.ToString("F2") + "%";
                    }
                }
                else
                {
                    MessageBox.Show("Pil bilgisi bulunamadı.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Pil bilgilerini alırken hata oluştu: " + ex.Message);
            }
        }

        private static ManagementObjectCollection WMIQuery(string query)
        {
            using (var searcher = new ManagementObjectSearcher(query))
            {
                return searcher.Get();
            }
        }
    }
}
Editor is loading...
Leave a Comment