( windowInfo: ExplorerFolderWindowInfo, logPrefix: string )
| 72 | * 返回 null 表示当前窗口不是可读取目录的 Explorer/桌面窗口。 |
| 73 | */ |
| 74 | export function getExplorerFolderPathFromWindow( |
| 75 | windowInfo: ExplorerFolderWindowInfo, |
| 76 | logPrefix: string |
| 77 | ): string | null { |
| 78 | if (windowInfo.className === 'Progman' || windowInfo.className === 'WorkerW') { |
| 79 | return app.getPath('desktop') |
| 80 | } |
| 81 | |
| 82 | if (windowInfo.className !== 'CabinetWClass' && windowInfo.className !== 'ExploreWClass') { |
| 83 | return null |
| 84 | } |
| 85 | |
| 86 | if (typeof windowInfo.hwnd !== 'number') { |
| 87 | console.error(`[${logPrefix}] Explorer 窗口缺少 hwnd,无法读取目录`) |
| 88 | return null |
| 89 | } |
| 90 | |
| 91 | let folderUrl: string | null = null |
| 92 | try { |
| 93 | folderUrl = WindowManager.getExplorerFolderPath(windowInfo.hwnd) |
| 94 | } catch (error) { |
| 95 | console.error(`[${logPrefix}] Explorer 目录读取异常 (hwnd=${windowInfo.hwnd}):`, error) |
| 96 | return null |
| 97 | } |
| 98 | |
| 99 | if (!folderUrl) { |
| 100 | console.error(`[${logPrefix}] Explorer 目录读取失败 (hwnd=${windowInfo.hwnd})`) |
| 101 | return null |
| 102 | } |
| 103 | |
| 104 | return decodeFileUrlToPath(folderUrl) |
| 105 | } |
no test coverage detected