You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#检测自开机后的首次Windows登录(适配Vista/7)

问题:判断Windows重启后的首次登录(非管理员权限)

我是C#编程新手,正在开发一款恢复Windows Vista及以上系统自定义启动音效的程序。程序以非管理员权限运行,无需作为服务,放在当前用户的自启动项中。程序仅在微软未播放登录音效时才播放自定义启动音效,判断逻辑如下:

if (CurrentUserHasAutoLogin == true || (CurrentUserAccountIsActiveOnly == true && NoPasswordSet == true && NotInDomain == true))
&& **SinceLastRestartItstheVeryFirstLogin == true**
{
    Play.Startup.Sound();
} else {
    // 不执行操作(Windows将播放默认登录音效)
}

目前逻辑已基本完成,但SinceLastRestartItstheVeryFirstLogin这一条件的实现遇到了困难。我已尝试的方案及需求如下:

  • 代码需至少兼容Windows Vista/7(目标框架为.NET 4.0);
  • 作为自启动程序,仅通过用户权限安装,无法在重新登录后留存;
  • 曾尝试调用WTSGetActiveConsoleSessionId,但在Windows Vista/7中,用户注销后重新登录时计数器会重置为0,无法准确判断;
  • 考虑过创建注册表/文件计数器,在首次登录后写入、关机时删除,但异常关机(如断电)会导致逻辑失效,且需避免依赖外部存储;
  • 曾设想通过查找系统进程SearchIndexer.exe判断,但部分用户会禁用Windows Search,导致该进程不会启动,此方案不可靠。

请问,在程序以非管理员权限运行且通过自启动项触发的情况下,如何判断当前是否为计算机自启动后的首次登录?


解决方案

可以通过对比系统启动时间与当前用户会话的创建时间来判断是否为重启后的首次登录:系统重启后第一个交互式用户会话的创建时间会非常接近系统启动时间,而注销后重新登录的会话创建时间会明显晚于系统启动时间。以下是兼容Windows Vista/7、.NET 4.0的非权限依赖实现:

1. 获取当前用户会话的创建时间

通过Windows API WTSQuerySessionInformation读取当前控制台会话的创建时间,非管理员权限可正常调用:

using System;
using System.Runtime.InteropServices;

public static class SessionHelper
{
    private const int WTS_CURRENT_SESSION = -1;
    private const int WTS_SESSION_INFO_LEVEL = 11; // 对应WTSCreateTime参数

    [DllImport("Wtsapi32.dll", SetLastError = true)]
    private static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, int infoClass, out IntPtr ppBuffer, out int pBytesReturned);

    [DllImport("Wtsapi32.dll")]
    private static extern void WTSFreeMemory(IntPtr pMemory);

    [StructLayout(LayoutKind.Sequential)]
    private struct FILETIME
    {
        public uint dwLowDateTime;
        public uint dwHighDateTime;
    }

    public static DateTime GetSessionCreationTime()
    {
        IntPtr buffer = IntPtr.Zero;
        int bytesReturned;
        try
        {
            if (WTSQuerySessionInformation(IntPtr.Zero, WTS_CURRENT_SESSION, WTS_SESSION_INFO_LEVEL, out buffer, out bytesReturned))
            {
                FILETIME ft = Marshal.PtrToStructure<FILETIME>(buffer);
                long fileTime = ((long)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
                return DateTime.FromFileTime(fileTime);
            }
            else
            {
                throw new System.ComponentModel.Win32Exception();
            }
        }
        finally
        {
            if (buffer != IntPtr.Zero)
                WTSFreeMemory(buffer);
        }
    }
}

2. 获取系统启动时间

通过PerformanceCounter读取系统运行时长,反推系统启动时间,非管理员权限可访问:

using System.Diagnostics;

public static DateTime GetSystemBootTime()
{
    using (var uptimeCounter = new PerformanceCounter("System", "System Up Time"))
    {
        uptimeCounter.NextValue(); // 首次调用返回0,需二次调用获取真实值
        double uptimeSeconds = uptimeCounter.NextValue();
        return DateTime.Now.AddSeconds(-uptimeSeconds);
    }
}

3. 实现首次登录判断逻辑

对比系统启动时间与会话创建时间的差值,设置合理阈值(比如5分钟),差值小于阈值则判定为首次登录:

public static bool IsFirstLoginAfterRestart()
{
    DateTime bootTime = GetSystemBootTime();
    DateTime sessionCreationTime = SessionHelper.GetSessionCreationTime();
    TimeSpan timeDiff = sessionCreationTime - bootTime;

    // 可根据实际场景调整阈值,5分钟足以覆盖绝大多数正常登录场景
    return timeDiff.TotalSeconds <= 300;
}

方案优势

  • 无外部存储依赖,避免异常关机导致的逻辑失效;
  • 所有API均可在非管理员权限下调用,符合程序运行要求;
  • 兼容Windows Vista/7及.NET 4.0框架,满足指定兼容性需求。

内容的提问来源于stack exchange,提问作者Dragodraki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 03:37:51