首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 開發多功能自動關機工具:定時、倒數、提醒與系統資訊

     編輯:關於C#

功能概覽

  • 定時關機(精確到秒)
  • 倒數計時關機/重啟/登出
  • 定時報警提醒
  • 系統資訊查詢(CPU、記憶體、路徑、時間)
  • 示範 C# 呼叫 Win32 API (P/Invoke) 技巧

介面設計

主表單 frmMain 包含:

  • 兩組 CheckBox:自動關機 / 倒數執行 / 定時報警
  • DateTimePicker 設定日期/時間
  • TextBox 輸入倒數秒數
  • ProgressBar、Label 顯示狀態
  • 按鈕:關機、重新啟動、登出、取消、系統資訊
  • Timer 元件 (Interval=100ms) 即時更新時間與檢查觸發條件

主介面

P/Invoke 呼叫 Win32 API

using System.Runtime.InteropServices;
using System.Text;

public class WinAPI
{
    [DllImport("user32.dll")]
    public static extern int ExitWindowsEx(int uFlags, int dwReserved);

    [DllImport("shell32.dll")]
    public static extern int ShellAbout(int hWnd, string szApp, string szOtherStuff, int hIcon);

    // 系統資訊用 API
    [DllImport("kernel32.dll")]
    public static extern void GetWindowsDirectory(StringBuilder lpBuffer, int uSize);
    [DllImport("kernel32.dll")]
    public static extern void GetSystemDirectory(StringBuilder lpBuffer, int uSize);
    [DllImport("kernel32.dll")]
    public static extern void GetSystemInfo(ref CPU_INFO lpSystemInfo);
    [DllImport("kernel32.dll")]
    public static extern void GlobalMemoryStatus(ref MEMORY_INFO lpBuffer);
    [DllImport("kernel32.dll")]
    public static extern void GetSystemTime(ref SYSTEMTIME lpSystemTime);
}

關機/重啟/登出旗標

const int EWX_LOGOFF = 0;
const int EWX_SHUTDOWN = 1;
const int EWX_REBOOT = 2;
const int EWX_FORCE = 4;

呼叫範例

private void Shutdown() => WinAPI.ExitWindowsEx(EWX_SHUTDOWN, 0);
private void Reboot() => WinAPI.ExitWindowsEx(EWX_REBOOT, 0);
private void Logoff() => WinAPI.ExitWindowsEx(EWX_LOGOFF, 0);

Timer 邏輯:即時檢查觸發條件

private void timer1_Tick(object sender, EventArgs e)
{
    string nowDate = DateTime.Now.ToShortDateString();
    string nowTime = DateTime.Now.ToLongTimeString();
    lblCurrentTime.Text = $"{nowDate} {nowTime}";

    // 定時關機檢查
    if (chkSchedule.Checked &&
        nowDate == dtpDate.Value.ToShortDateString() &&
        nowTime == dtpTime.Value.ToLongTimeString())
    {
        Shutdown();
    }

    // 倒數計時邏輯(略)...
}

系統資訊視窗 (Form2)

定義三個結構對應 API 回傳資料:

[StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
    public uint dwOemId, dwPageSize, lpMinimumApplicationAddress,
                lpMaximumApplicationAddress, dwActiveProcessorMask,
                dwNumberOfProcessors, dwProcessorType, dwAllocationGranularity,
                dwProcessorLevel, dwProcessorRevision;
}

[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
    public uint dwLength, dwMemoryLoad, dwTotalPhys, dwAvailPhys,
                dwTotalPageFile, dwAvailPageFile, dwTotalVirtual, dwAvailVirtual;
}

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
    public ushort wYear, wMonth, wDayOfWeek, wDay,
                  wHour, wMinute, wSecond, wMilliseconds;
}

按鈕點擊一次性查詢並填入介面:

private void btnQuerySysInfo_Click(object sender, EventArgs e)
{
    // Windows/系統路徑
    var sb = new StringBuilder(128);
    WinAPI.GetWindowsDirectory(sb, 128);
    txtWinDir.Text = "Windows路徑:" + sb.ToString();
    WinAPI.GetSystemDirectory(sb, 128);
    txtSysDir.Text = "系統路徑:" + sb.ToString();

    // CPU 資訊
    CPU_INFO cpu = new CPU_INFO();
    WinAPI.GetSystemInfo(ref cpu);
    txtCpuCount.Text = $"CPU 數量:{cpu.dwNumberOfProcessors}";
    txtCpuType.Text = $"CPU 型別:{cpu.dwProcessorType}";
    txtCpuLevel.Text = $"CPU 等級:{cpu.dwProcessorLevel}";

    // 記憶體資訊
    MEMORY_INFO mem = new MEMORY_INFO { dwLength = (uint)Marshal.SizeOf(typeof(MEMORY_INFO)) };
    WinAPI.GlobalMemoryStatus(ref mem);
    txtMemLoad.Text = $"{mem.dwMemoryLoad}% 使用中";
    txtTotalPhys.Text = $"實體記憶體:{mem.dwTotalPhys} 位元組";
    txtAvailPhys.Text = $"可用實體:{mem.dwAvailPhys} 位元組";

    // 系統時間 (UTC,需 +8 小時)
    SYSTEMTIME st = new SYSTEMTIME();
    WinAPI.GetSystemTime(ref st);
    txtDate.Text = $"{st.wYear}年{st.wMonth}月{st.wDay}日";
    txtTime.Text = $"{(st.wHour + 8) % 24}時{st.wMinute}分{st.wSecond}秒";
}

系統資訊介面

總結

本專案完整示範 C# 透過 P/Invoke 呼叫 user32.dll、kernel32.dll、shell32.dll 實現系統級操作,涵蓋關機控制、系統資訊查詢、定時器邏輯與 WinForms 介面設計。有 API 基礎的開發者可快速舉一反三,延伸更多系統管理功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved