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

如何获取File Explorer窗口的启动时间?(AutoHotkey脚本开发)

获取File Explorer窗口的启动时间(AutoHotkey实现)

我来帮你解决这个问题——你原来用window.Document.Folder.Self.Time是拿错了属性,这个返回的是文件夹本身的修改时间,和窗口打开时间完全没关系。下面给你两种可行的实现方式,以及一些需要注意的限制:

方案1:获取窗口所属进程的启动时间(简单易用)

这个方法通过窗口的HWND获取对应的explorer.exe进程ID,再拿到进程的启动时间。优点是代码简单,缺点是如果多个File Explorer窗口属于同一个进程(默认情况就是这样),所有窗口的时间会完全相同。

修改后的完整函数:

list_opened_folders(byref file_explorer_windows) {
    file_explorer_windows := []
    for window in ComObjCreate("Shell.Application").Windows {
        ; 过滤掉非File Explorer窗口(比如IE窗口)
        if (window.Name != "File Explorer" && window.Name != "Windows Explorer")
            continue

        hwnd := window.HWND
        file_explorer_windows[A_Index] := {}
        file_explorer_windows[A_Index].path := window.Document.Folder.Self.Path
        file_explorer_windows[A_Index].id := hwnd
        
        ; 获取进程ID
        WinGetPID, pid, ahk_id %hwnd%
        ; 获取进程创建时间并格式化
        Process, GetCreationTime,, %pid%, creationTime
        file_explorer_windows[A_Index].started_time := Format("{4}-{2}-{3} {5}:{6}:{7}", creationTime*)
    }
}

方案2:通过API获取进程创建时间(更灵活)

和方案1本质一样,但用Windows API实现,适合需要更多自定义的场景。同样,它获取的还是进程启动时间,不是单个窗口的精确打开时间:

; 封装获取进程创建时间的函数
GetProcessCreationTime(hWnd) {
    ; 获取窗口所属进程ID
    DllCall("user32.dll\GetWindowThreadProcessId", "Ptr", hWnd, "UInt*", pid)
    ; 打开进程(需要查询权限)
    hProcess := DllCall("kernel32.dll\OpenProcess", "UInt", 0x0400 | 0x0010, "Int", 0, "UInt", pid, "Ptr")
    if (!hProcess)
        return "获取失败"
    
    ; 获取进程时间(FILETIME格式)
    VarSetCapacity(creationTime, 8, 0)
    VarSetCapacity(ignore, 8, 0)
    success := DllCall("kernel32.dll\GetProcessTimes", "Ptr", hProcess, "Ptr", &creationTime, "Ptr", &ignore, "Ptr", &ignore, "Ptr", &ignore)
    DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess)
    
    if (!success)
        return "获取失败"
    
    ; 转换为可读的系统时间
    VarSetCapacity(systemTime, 16, 0)
    DllCall("kernel32.dll\FileTimeToSystemTime", "Ptr", &creationTime, "Ptr", &systemTime)
    
    year := NumGet(systemTime, 0, "UShort")
    month := NumGet(systemTime, 2, "UShort")
    day := NumGet(systemTime, 6, "UShort")
    hour := NumGet(systemTime, 8, "UShort")
    minute := NumGet(systemTime, 10, "UShort")
    second := NumGet(systemTime, 12, "UShort")
    
    return Format("{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}", year, month, day, hour, minute, second)
}

list_opened_folders(byref file_explorer_windows) {
    file_explorer_windows := []
    for window in ComObjCreate("Shell.Application").Windows {
        if (window.Name != "File Explorer" && window.Name != "Windows Explorer")
            continue

        hwnd := window.HWND
        file_explorer_windows[A_Index] := {}
        file_explorer_windows[A_Index].path := window.Document.Folder.Self.Path
        file_explorer_windows[A_Index].id := hwnd
        ; 调用自定义函数获取时间
        file_explorer_windows[A_Index].started_time := GetProcessCreationTime(hwnd)
    }
}

重要说明:单个窗口精确打开时间的限制

Windows并没有提供直接查询已打开窗口精确创建时间的API,因为Shell.Window对象不包含这个属性,系统也没有针对单个窗口的时间查询接口。如果你的需求是记录未来新打开的File Explorer窗口的精确时间,可以用窗口创建钩子实现:

#Persistent
; 设置钩子监听窗口创建事件
SetWinEventHook(0x8000, 0x8000, 0, RegisterCallback("OnNewExplorerWindow"), 0, 0, 0x0000)
return

OnNewExplorerWindow(hHook, event, hwnd, idObj, idChild, dwThread, dwTime) {
    ; 只处理窗口对象
    if (idObj != 0)
        return
    
    ; 验证是否是File Explorer窗口
    WinGet, procName, ProcessName, ahk_id %hwnd%
    WinGet, className, ClassName, ahk_id %hwnd%
    if (procName != "explorer.exe" || className != "CabinetWClass")
        return
    
    ; 获取当前时间作为窗口打开时间
    openTime := A_Now
    ; 获取窗口路径
    for window in ComObjCreate("Shell.Application").Windows {
        if (window.HWND == hwnd) {
            folderPath := window.Document.Folder.Self.Path
            break
        }
    }
    
    ; 这里可以把记录保存到数组或文件中
    ToolTip, 新窗口打开时间: %openTime%`n路径: %folderPath%
    SetTimer, ToolTip, -2500
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:28:23