Skip to content

Commit 6cbd03a

Browse files
Copilotccmywish
andcommitted
Fix Windows downloads directory recognition in installer.ps1
- Changed default directory parameter from hardcoded ${HOME}\Downloads to null - Added Get_Downloads_Dir function to read actual downloads folder from Windows registry - Modified Set_Install_Dir to auto-detect downloads directory or use current directory - Updated help text to reflect new default behavior (current directory) - Removed unused $default_install_dir variable Co-authored-by: ccmywish <63459097+ccmywish@users.noreply.github.com>
1 parent f4f6835 commit 6cbd03a

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

tool/installer.ps1

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Contributors : Aoran Zeng <ccmywish@qq.com>
88
# |
99
# Created On : <2024-10-26>
10-
# Last Modified : <2025-03-07>
10+
# Last Modified : <2026-01-06>
1111
#
1212
# chsrc installer for Windows
1313
# ---------------------------------------------------------------
@@ -16,13 +16,12 @@
1616
param(
1717
[switch]
1818
$Help,
19-
$Directory = "${HOME}\Downloads",
19+
$Directory = $null,
2020
$Version = "pre"
2121
)
2222

2323

2424
$binary_name = "chsrc"
25-
$default_install_dir = "${HOME}\Downloads"
2625
$platform = "Windows"
2726

2827
$global:install_dir = ""
@@ -58,7 +57,7 @@ chsrc-installer: Install chsrc on ${platform}.
5857
Usage: install.ps1 [options]
5958
Options:
6059
-h Print this help information
61-
-d <dir> Specify installation directory, default is $default_install_dir
60+
-d <dir> Specify installation directory, default is current directory
6261
-v <version> Specify chsrc version
6362
6463
"@
@@ -80,7 +79,42 @@ function output_error () {
8079
}
8180

8281

82+
function Get_Downloads_Dir {
83+
# 尝试从注册表获取实际的 Downloads 文件夹位置
84+
try {
85+
$shellFoldersKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
86+
$downloadsGuid = "{374DE290-123F-4565-9164-39C4925E467B}"
87+
$downloadsPath = (Get-ItemProperty -Path $shellFoldersKey -Name $downloadsGuid -ErrorAction Stop).$downloadsGuid
88+
89+
# 展开环境变量 (例如 %USERPROFILE% -> C:\Users\xxx)
90+
$downloadsPath = [System.Environment]::ExpandEnvironmentVariables($downloadsPath)
91+
92+
if (Test-Path -Path $downloadsPath -PathType Container) {
93+
return $downloadsPath
94+
}
95+
} catch {
96+
# 如果注册表读取失败,不输出错误信息,继续使用后备方案
97+
}
98+
99+
# 后备方案:返回 null,稍后使用当前目录
100+
return $null
101+
}
102+
83103
function Set_Install_Dir {
104+
# 如果用户未指定目录,则自动检测
105+
if ($null -eq $Directory) {
106+
# 尝试获取实际的 Downloads 目录
107+
$detectedDownloads = Get_Downloads_Dir
108+
if ($null -ne $detectedDownloads) {
109+
$Directory = $detectedDownloads
110+
output_info "Detected Downloads directory: $Directory"
111+
} else {
112+
# 使用当前目录作为默认值
113+
$Directory = Get-Location | Select-Object -ExpandProperty Path
114+
output_info "Using current directory: $Directory"
115+
}
116+
}
117+
84118
# 检查目录是否存在
85119
if (-not (Test-Path -Path $Directory -PathType Container)) {
86120
# 如果目录不存在,执行下面的代码块

0 commit comments

Comments
 (0)