各版install.ps1

· · 个人记录

vscode 一键配置工具 by herunqing

通用版

# 定义文件和目录 
$VSCodeInstaller = "VSCodeUserSetup-x64-1.115.0.exe"  
$MinGWArchive = "mingw.zip" # https://github.com/niXman/mingw-builds-binaries/releases
$MinGWTargetDir = "D:\mingw"
$files = @(
    "ms-vscode.cpptools-1.31.4-win32-x64.vsix",
    "ms-ceintl.vscode-language-pack-zh-hans-1.110.2026041514.vsix",
    "pomdtr.excalidraw-editor-3.9.1.vsix",
    "shalldie.background-2.0.10.vsix",
    "formulahendry.code-runner-0.12.2.vsix",
    "yltx.vscode-luogu-4.18.2.vsix",
    "langningchen.cph-ng-0.7.11.vsix"
)
$font = "MapleMono-NF-CN-Medium.ttf"  # https://github.com/tonsky/FiraCode/releases/tag/6.2
$fontFamily = "Maple Mono NF CN Medium"

# 获取脚本所在的当前目录
$CurrentDir = (Split-Path -Parent $MyInvocation.MyCommand.Definition)
$VSCodePath = Join-Path -Path $CurrentDir -ChildPath $VSCodeInstaller
$MinGWArchivePath = Join-Path -Path $CurrentDir -ChildPath $MinGWArchive

# --- 1. 安装 VS Code ---
Write-Host "--- 1. 安装 VS Code ---" -ForegroundColor Cyan

# 检查 VS Code 安装文件是否存在
if (-not (Test-Path $VSCodePath)) {
    Write-Host "错误:未找到文件 $VSCodeInstaller 在 $CurrentDir。" -ForegroundColor Red
} else {
    # 静默安装 VS Code。/VERYSILENT /SUPPRESSMSGBOXES 是常用的静默安装参数。
    Write-Host "正在静默安装 VS Code..." -ForegroundColor Yellow
    # 使用 Start-Process 运行安装程序并等待其完成
    try {
        # 注意:UserSetup版本通常使用 Inno Setup,支持 /VERYSILENT
        Start-Process -FilePath $VSCodePath -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /MERGETASKS=!runcode" -Wait -NoNewWindow
        Write-Host "VS Code 安装完成。" -ForegroundColor Green
    } catch {
        Write-Host "VS Code 安装过程中发生错误:$($_.Exception.Message)" -ForegroundColor Red
    }
}

Write-Host "`n--- 2. 解压 MinGW ---" -ForegroundColor Cyan

# 检查 MinGW 压缩文件是否存在
if (-not (Test-Path $MinGWArchivePath)) {
    Write-Host "错误:未找到文件 $MinGWArchive 在 $CurrentDir。" -ForegroundColor Red
} else {
    # 检查目标目录是否存在,如果不存在则创建
    if (-not (Test-Path $MinGWTargetDir)) {
        Write-Host "目标目录 $MinGWTargetDir 不存在,正在创建..." -ForegroundColor Yellow
        New-Item -Path $MinGWTargetDir -ItemType Directory | Out-Null
    }

    Write-Host "正在解压 $MinGWArchive 到 $MinGWTargetDir..." -ForegroundColor Yellow

    try {
        Expand-Archive -Path $MinGWArchivePath -DestinationPath $MinGWTargetDir -Force

        # 检查解压是否成功 (通过检查目标目录是否非空来简单判断)
        if ((Get-ChildItem -Path $MinGWTargetDir).Count -gt 0) {
            Write-Host "MinGW 解压完成。" -ForegroundColor Green
        } else {
            Write-Host "警告:MinGW 似乎没有被成功解压到目标目录。" -ForegroundColor Yellow
        }

    } catch {
        Write-Host "提取过程中发生错误。" -ForegroundColor Red
        Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red
    }
}

# --- 3. 添加 MinGW 到环境变量 ---
Write-Host "`n--- 3. 添加 MinGW 到环境变量 ---" -ForegroundColor Cyan

# 假设 MinGW 的可执行文件(如 gcc.exe)位于 C:\mingw\bin 目录下。
# 如果你的 MinGW 结构不同,请修改此路径。
$MinGWBinDir = Join-Path -Path $MinGWTargetDir -ChildPath "bin"

# 检查 MinGW/bin 目录是否存在
if (-not (Test-Path $MinGWBinDir -PathType Container)) {
    Write-Host "错误:MinGW 可执行文件目录 $MinGWBinDir 不存在。请检查 MinGW 的解压内容。" -ForegroundColor Red
} else {
    # 获取当前的系统 Path 变量
    $CurrentPath = [Environment]::GetEnvironmentVariable("Path", "Machine")

    # 检查 MinGW 路径是否已存在于 Path 中
    # 使用 -clike 运算符进行不区分大小写的模式匹配
    if ($CurrentPath -clike "*$MinGWBinDir*") {
        Write-Host "路径 $MinGWBinDir 已存在于系统 Path 环境变量中。" -ForegroundColor Yellow
    } else {
        # 添加 MinGW bin 目录到系统 Path 环境变量
        Write-Host "正在将 $MinGWBinDir 添加到系统 Path 环境变量..." -ForegroundColor Yellow

        # 使用 [Environment]::SetEnvironmentVariable 修改 Path
        # "Machine" 表示对所有用户生效 (系统环境变量)。
        $NewPath = "$CurrentPath;$MinGWBinDir"
        [Environment]::SetEnvironmentVariable("Path", $NewPath, "Machine")

        Write-Host "Path 环境变量更新完成。" -ForegroundColor Green
        Write-Host "注意:新的环境变量将在新的命令行或PowerShell会话中生效。" -ForegroundColor Yellow
    }
}


$installPath = "$env:LOCALAPPDATA\Programs\Microsoft VS Code\bin"


foreach ($f in $files) {  & "$installPath\code.cmd" --install-extension $f }

try{
    Get-Process -Name Code | Stop-Process -Force
}
catch{}

$dest = "C:\Windows\Fonts"

# 复制字体到系统目录
Copy-Item $font -Destination "$dest\$font" -Force

# 注册到系统字体注册表
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
New-ItemProperty -Path $regPath `
    -Name "$fontFamily (TrueType)" `
    -Value $font `
    -PropertyType String -Force

# 调用 Windows API 添加到当前会话
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class FontUtil {
    [DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)]
    public static extern int AddFontResource(string lpFileName);

    [DllImport("user32.dll", SetLastError=true)]
    public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
"@

[FontUtil]::AddFontResource("$dest\$font") | Out-Null

# 通知系统刷新字体表
$HWND_BROADCAST = [intptr]0xffff
$WM_FONTCHANGE = 0x001D
[FontUtil]::SendMessage($HWND_BROADCAST, $WM_FONTCHANGE, [intptr]0, [intptr]0)

Write-Host "字体 Fira Code 已安装并刷新系统"
# 等待安装完成后,复制配置文件

$content = @"
{
    "editor.smoothScrolling": true,
    "editor.cursorBlinking": "expand",
    "editor.guides.bracketPairs": true,
    "editor.bracketPairColorization.enabled": true,
    "editor.acceptSuggestionOnEnter": "smart",
    "editor.suggestSelection": "recentlyUsedByPrefix",
    "editor.autoClosingDelete": "always",
    "editor.autoClosingOvertype": "always",

    "editor.fontSize": 18,
    "editor.fontFamily": "'Maple Mono NF CN Medium', monospace",
    "editor.fontLigatures": true,
    "editor.formatOnSave": true,
    "extensions.ignoreRecommendations": true,
    "editor.mouseWheelZoom": true,
    "editor.cursorSmoothCaretAnimation": "on",
    "files.autoGuessEncoding": true,
    "C_Cpp.clang_format_fallbackStyle": "{ColumnLimit: 170, BasedOnStyle: LLVM, IndentWidth: 4,  TabWidth: 4 }",
    "workbench.startupEditor": "none",
    "security.workspace.trust.untrustedFiles": "open",
    "C_Cpp.default.compilerPath": "D:/mingw/bin/g++.exe",
    "files.associations": {
        "*.cpp": "cpp"
    },
    "[cpp]": {
        "editor.wordBasedSuggestions": "off",
        "editor.semanticHighlighting.enabled": true,
        "editor.stickyScroll.defaultModel": "foldingProviderModel",
        "editor.suggest.insertMode": "replace"
    },
    "explorer.confirmDelete": false,
    "json.format.keepLines": true,
    "background.fullscreen": {
        "images": [
            // "https://upload-bbs.miyoushe.com/upload/2026/04/26/404580670/f9699cd0b02044a5c167b1efe554a088_6490500312537179773.jpg",
            // "https://cdn.luogu.com.cn/upload/image_hosting/aiu0h201.png",
            // "https://upload-bbs.miyoushe.com/upload/2024/01/18/311370925/de6edf6312b5c2ee4e0d14f70ba6234e_9009544746652854059.jpg"
            "https://album.biliimg.com/bfs/new_dyn/4256c9b037af54280d186ef08a23e55f1349174610.jpg"
        ],
        "opacity": 0.3,
        "size": "cover",
        "position": "center",
        "interval": 60,
        "random": true
    },
    "background.sidebar": { },
    "background.panel": { },
    "update.showReleaseNotes": false,
    "background.enabled": true,
    "excalidraw.theme": "auto",
    "explorer.confirmDragAndDrop": false,
    "workbench.browser.showInTitleBar": true,
    "workbench.editor.empty.hint": "hidden",
    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {

        // 使用 python3 运行 Python 文件
        "python": "python -u",
        // 使用 g++ 编译并运行 C++ 文件
        "cpp": "cd `$dir && g++ -Wall -static -Wextra -O2 `$fileName -o `$fileNameWithoutExt && `$dir`$fileNameWithoutExt",
        // 使用 node 运行 JavaScript 文件
        "javascript": "node",
        // 使用 dotnet run 运行 C# 项目
        "csharp": "cd `$dir && dotnet run"
    },
    "cph-ng.languages.cppCompiler": "D:\\mingw\\bin\\g++.EXE",
    "cph-ng.languages.cppCompilerArgs": "-O2 -std=c++14 -Wall -DCPH -static",
    "cph-ng.sidebar.showOobe": false,
    "workbench.editor.closeEmptyGroups": false
}
"@


$argv = @"
{
    "locale": "zh-cn",
    "enable-crash-reporter": true,
    "crash-reporter-id": "$([guid]::NewGuid().ToString())"
}
"@
$argvPath = "$env:USERPROFILE\.vscode\argv.json"

try{
    $argv | Set-Content -Path $argvPath
}
catch{
    Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red
}
$targetDir = "$env:APPDATA\Code\User"
$target = "$targetDir\settings.json"

# 如果目标目录不存在则创建
if (-not (Test-Path $targetDir)) {
    New-Item -ItemType Directory -Path $targetDir | Out-Null
}
try{
    $content | Set-Content -Path $target -Encoding UTF8
    Write-Host "VSCode 安装完成并成功应用自定义设置" -ForegroundColor Green
}
catch{
    Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red
}


Write-Host "`n--- 脚本执行完毕 ---" -ForegroundColor Cyan

pause

SES集训专用版

MinGW 安装目录(用于添加环境变量,请确认该目录存在)

$MinGWBinDir = Join-Path -Path $MinGWTargetDir -ChildPath "bin" # VS Code 可执行文件目录(用于安装扩展,请确认 code.cmd 所在路径) $VSCodeBinDir = "D:\Programs\Microsoft VS Code\bin" # 若默认安装,可为 "$env:LOCALAPPDATA\Programs\Microsoft VS Code\bin" # 扩展文件列表(需放在脚本同目录下) $files = @( "ms-vscode.cpptools-1.31.4-win32-x64.vsix", "ms-ceintl.vscode-language-pack-zh-hans-1.110.2026041514.vsix", "pomdtr.excalidraw-editor-3.9.1.vsix", "shalldie.background-2.0.10.vsix", "formulahendry.code-runner-0.12.2.vsix", "yltx.vscode-luogu-4.14.0.vsix" ) # 字体文件(需放在脚本同目录下) $font = "FiraCode-Regular.ttf" $fontFamily = "Fira Code" # ============================================= # 1. 添加 MinGW 到系统环境变量 Path # ============================================= Write-Host "--- 1. 添加 MinGW 到环境变量 ---" -ForegroundColor Cyan if (-not (Test-Path $MinGWBinDir -PathType Container)) { Write-Host "警告:MinGW 可执行目录 $MinGWBinDir 不存在,请确认路径正确。" -ForegroundColor Yellow } else { $CurrentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") if ($CurrentPath -clike "*$MinGWBinDir*") { Write-Host "路径 $MinGWBinDir 已存在于系统 Path 中。" -ForegroundColor Yellow } else { Write-Host "正在添加 $MinGWBinDir 到系统 Path ..." -ForegroundColor Yellow $NewPath = "$CurrentPath;$MinGWBinDir" [Environment]::SetEnvironmentVariable("Path", $NewPath, "Machine") Write-Host "Path 更新完成。新会话生效。" -ForegroundColor Green } } # ============================================= # 2. 安装 Fira Code 字体 # ============================================= Write-Host "`n--- 2. 安装 Fira Code 字体 ---" -ForegroundColor Cyan $dest = "C:\Windows\Fonts" if (-not (Test-Path $font)) { Write-Host "错误:字体文件 $font 未找到,跳过安装。" -ForegroundColor Red } else { Copy-Item $font -Destination "$dest\$font" -Force $regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" New-ItemProperty -Path $regPath -Name "$fontFamily (TrueType)" -Value $font -PropertyType String -Force # 加载字体到当前会话 Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; public class FontUtil { [DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)] public static extern int AddFontResource(string lpFileName); [DllImport("user32.dll", SetLastError=true)] public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); } "@ [FontUtil]::AddFontResource("$dest\$font") | Out-Null $HWND_BROADCAST = [intptr]0xffff $WM_FONTCHANGE = 0x001D [FontUtil]::SendMessage($HWND_BROADCAST, $WM_FONTCHANGE, [intptr]0, [intptr]0) Write-Host "字体已安装并刷新系统。" -ForegroundColor Green } # ============================================= # 3. 安装 VS Code 扩展 # ============================================= Write-Host "`n--- 3. 安装 VS Code 扩展 ---" -ForegroundColor Cyan # 检查 code 命令是否可用 $codeCmd = Join-Path -Path $VSCodeBinDir -ChildPath "code.cmd" if (-not (Test-Path $codeCmd)) { Write-Host "错误:未找到 code.cmd(路径:$codeCmd),请修改 `$VSCodeBinDir 变量。" -ForegroundColor Red } else { foreach ($f in $files) { if (Test-Path $f) { Write-Host "安装扩展:$f" -ForegroundColor Yellow & $codeCmd --install-extension $f } else { Write-Host "警告:扩展文件 $f 不存在,跳过。" -ForegroundColor Yellow } } # 关闭已打开的 VS Code,避免扩展加载冲突 try { Get-Process -Name Code | Stop-Process -Force Write-Host "已关闭正在运行的 VS Code。" -ForegroundColor Gray } catch {} } # ============================================= # 4. 写入 VS Code 配置文件 (settings.json & argv.json) # ============================================= Write-Host "`n--- 4. 配置 VS Code 设置 ---" -ForegroundColor Cyan $content = @" { "editor.smoothScrolling": true, "editor.cursorBlinking": "expand", "editor.guides.bracketPairs": true, "editor.bracketPairColorization.enabled": true, "editor.acceptSuggestionOnEnter": "smart", "editor.suggestSelection": "recentlyUsedByPrefix", "editor.autoClosingBrackets": "beforeWhitespace", "editor.autoClosingQuotes": "beforeWhitespace", "editor.autoClosingDelete": "always", "editor.autoClosingOvertype": "always", "editor.fontSize": 18, "editor.fontFamily": "'Fira Code', monospace", "editor.fontLigatures": true, "editor.formatOnSave": true, "extensions.ignoreRecommendations": true, "editor.mouseWheelZoom": true, "editor.cursorSmoothCaretAnimation": "on", "files.autoGuessEncoding": true, "C_Cpp.clang_format_fallbackStyle": "{ColumnLimit: 70, BasedOnStyle: LLVM, IndentWidth: 4, TabWidth: 4 }", "workbench.startupEditor": "none", "security.workspace.trust.untrustedFiles": "open", "C_Cpp.default.compilerPath": "D:/mingw/bin/g++.exe", "files.associations": { "*.cpp": "cpp" }, "[cpp]": { "editor.wordBasedSuggestions": "off", "editor.semanticHighlighting.enabled": true, "editor.stickyScroll.defaultModel": "foldingProviderModel", "editor.suggest.insertMode": "replace" }, "explorer.confirmDelete": false, "json.format.keepLines": true, "background.fullscreen": { "images": [ // "https://upload-bbs.miyoushe.com/upload/2026/04/26/404580670/f9699cd0b02044a5c167b1efe554a088_6490500312537179773.jpg", // "https://cdn.luogu.com.cn/upload/image_hosting/aiu0h201.png", // "https://upload-bbs.miyoushe.com/upload/2024/01/18/311370925/de6edf6312b5c2ee4e0d14f70ba6234e_9009544746652854059.jpg" "https://album.biliimg.com/bfs/new_dyn/4256c9b037af54280d186ef08a23e55f1349174610.jpg" ], "opacity": 0.3, "size": "cover", "position": "center", "interval": 60, "random": true }, "background.sidebar": { }, "background.panel": { }, "update.showReleaseNotes": false, "background.enabled": true, "excalidraw.theme": "auto", "explorer.confirmDragAndDrop": false, "workbench.browser.showInTitleBar": true, "workbench.editor.empty.hint": "hidden", "code-runner.runInTerminal": true, "code-runner.executorMap": { // 使用 python3 运行 Python 文件 "python": "python3 -u", // 使用 g++ 编译并运行 C++ 文件 "cpp": "cd `$dir && g++ -Wall -Wextra -O2 `$fileName -o `$fileNameWithoutExt && `$dir`$fileNameWithoutExt", // 使用 node 运行 JavaScript 文件 "javascript": "node", // 使用 dotnet run 运行 C# 项目 "csharp": "cd `$dir && dotnet run" } } "@ $argv = @" { "locale": "zh-cn", "enable-crash-reporter": true, "crash-reporter-id": "$([guid]::NewGuid().ToString())" } "@ $argvPath = "$env:USERPROFILE\.vscode\argv.json" try{ $argv | Set-Content -Path $argvPath } catch{ Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red } $targetDir = "$env:APPDATA\Code\User" $target = "$targetDir\settings.json" # 如果目标目录不存在则创建 if (-not (Test-Path $targetDir)) { New-Item -ItemType Directory -Path $targetDir | Out-Null } try{ $content | Set-Content -Path $target -Encoding UTF8 Write-Host "VSCode 安装完成并成功应用自定义设置" -ForegroundColor Green } catch{ Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red } Write-Host "`n--- 脚本执行完毕 ---" -ForegroundColor Cyan pause ``` - B ```ps1 # ============================================= # 定义所需文件及路径(请根据实际情况调整) # ============================================= # MinGW 安装目录(用于添加环境变量,请确认该目录存在) $MinGWTargetDir = "D:\mingw" # 改为你的 MinGW 实际路径 $MinGWBinDir = Join-Path -Path $MinGWTargetDir -ChildPath "bin" # VS Code 可执行文件目录(用于安装扩展,请确认 code.cmd 所在路径) $VSCodeBinDir = "D:\Programs\Microsoft VS Code\bin" # 若默认安装,可为 "$env:LOCALAPPDATA\Programs\Microsoft VS Code\bin" # 扩展文件列表(需放在脚本同目录下) $files = @( "ms-vscode.cpptools-1.31.4-win32-x64.vsix", "ms-ceintl.vscode-language-pack-zh-hans-1.110.2026041514.vsix", "pomdtr.excalidraw-editor-3.9.1.vsix", "shalldie.background-2.0.10.vsix", "formulahendry.code-runner-0.12.2.vsix", "yltx.vscode-luogu-4.14.0.vsix" ) # 字体文件(需放在脚本同目录下) $fonts = @( "FiraCode-Regular.ttf", "MapleMono-NF-CN-Medium.ttf" ) # ============================================= # 1. 添加 MinGW 到系统环境变量 Path # ============================================= Write-Host "--- 1. 添加 MinGW 到环境变量 ---" -ForegroundColor Cyan if (-not (Test-Path $MinGWBinDir -PathType Container)) { Write-Host "警告:MinGW 可执行目录 $MinGWBinDir 不存在,请确认路径正确。" -ForegroundColor Yellow } else { $CurrentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") if ($CurrentPath -clike "*$MinGWBinDir*") { Write-Host "路径 $MinGWBinDir 已存在于系统 Path 中。" -ForegroundColor Yellow } else { Write-Host "正在添加 $MinGWBinDir 到系统 Path ..." -ForegroundColor Yellow $NewPath = "$CurrentPath;$MinGWBinDir" [Environment]::SetEnvironmentVariable("Path", $NewPath, "Machine") Write-Host "Path 更新完成。新会话生效。" -ForegroundColor Green } } # ============================================= # 2. 安装多个字体(使用 Shell.Application) # ============================================= Write-Host "`n--- 2. 安装字体 ---" -ForegroundColor Cyan # 加载 Shell.Application 用于安装字体 $shell = New-Object -ComObject Shell.Application $fontsFolder = $shell.NameSpace(0x14) # 0x14 是 Fonts 文件夹的 CSIDL foreach ($fontFile in $fonts) { if (-not (Test-Path $fontFile)) { Write-Host "警告:字体文件 $fontFile 不存在,跳过。" -ForegroundColor Yellow continue } # 复制到 Fonts 目录(Shell 会自动注册) $destFile = Join-Path "C:\Windows\Fonts" $fontFile Copy-Item $fontFile -Destination $destFile -Force # 使用 Shell 安装字体(推荐,会自动写入注册表) $fontsFolder.CopyHere($destFile, 0x10) # 0x10 表示如果存在则替换 Write-Host "已安装字体:$fontFile" -ForegroundColor Green } # 发送字体更改通知(让正在运行的程序刷新字体列表) Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; public class FontUtil { [DllImport("user32.dll", SetLastError=true)] public static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); } "@ $HWND_BROADCAST = [intptr]0xffff $WM_FONTCHANGE = 0x001D [FontUtil]::SendMessage($HWND_BROADCAST, $WM_FONTCHANGE, [intptr]0, [intptr]0) Write-Host "字体已安装并刷新系统。" -ForegroundColor Green # ============================================= # 3. 安装 VS Code 扩展 # ============================================= Write-Host "`n--- 3. 安装 VS Code 扩展 ---" -ForegroundColor Cyan # 检查 code 命令是否可用 $codeCmd = Join-Path -Path $VSCodeBinDir -ChildPath "code.cmd" if (-not (Test-Path $codeCmd)) { Write-Host "错误:未找到 code.cmd(路径:$codeCmd),请修改 `$VSCodeBinDir 变量。" -ForegroundColor Red } else { foreach ($f in $files) { if (Test-Path $f) { Write-Host "安装扩展:$f" -ForegroundColor Yellow & $codeCmd --install-extension $f } else { Write-Host "警告:扩展文件 $f 不存在,跳过。" -ForegroundColor Yellow } } # 关闭已打开的 VS Code,避免扩展加载冲突 try { Get-Process -Name Code | Stop-Process -Force Write-Host "已关闭正在运行的 VS Code。" -ForegroundColor Gray } catch {} } # ============================================= # 4. 写入 VS Code 配置文件 (settings.json & argv.json) # ============================================= Write-Host "`n--- 4. 配置 VS Code 设置 ---" -ForegroundColor Cyan $content = @" { "editor.smoothScrolling": true, "editor.cursorBlinking": "expand", "editor.guides.bracketPairs": true, "editor.bracketPairColorization.enabled": true, "editor.acceptSuggestionOnEnter": "smart", "editor.suggestSelection": "recentlyUsedByPrefix", "editor.autoClosingBrackets": "beforeWhitespace", "editor.autoClosingQuotes": "beforeWhitespace", "editor.autoClosingDelete": "always", "editor.autoClosingOvertype": "always", "editor.fontSize": 18, "editor.fontFamily": "'Fira Code', monospace", "editor.fontLigatures": true, "editor.formatOnSave": true, "extensions.ignoreRecommendations": true, "editor.mouseWheelZoom": true, "editor.cursorSmoothCaretAnimation": "on", "files.autoGuessEncoding": true, "C_Cpp.clang_format_fallbackStyle": "{ColumnLimit: 70, BasedOnStyle: LLVM, IndentWidth: 4, TabWidth: 4 }", "workbench.startupEditor": "none", "security.workspace.trust.untrustedFiles": "open", "C_Cpp.default.compilerPath": "D:/mingw/bin/g++.exe", "files.associations": { "*.cpp": "cpp" }, "[cpp]": { "editor.wordBasedSuggestions": "off", "editor.semanticHighlighting.enabled": true, "editor.stickyScroll.defaultModel": "foldingProviderModel", "editor.suggest.insertMode": "replace" }, "explorer.confirmDelete": false, "json.format.keepLines": true, "background.fullscreen": { "images": [ // "https://upload-bbs.miyoushe.com/upload/2026/04/26/404580670/f9699cd0b02044a5c167b1efe554a088_6490500312537179773.jpg", // "https://cdn.luogu.com.cn/upload/image_hosting/aiu0h201.png", // "https://upload-bbs.miyoushe.com/upload/2024/01/18/311370925/de6edf6312b5c2ee4e0d14f70ba6234e_9009544746652854059.jpg" "https://album.biliimg.com/bfs/new_dyn/4256c9b037af54280d186ef08a23e55f1349174610.jpg" ], "opacity": 0.3, "size": "cover", "position": "center", "interval": 60, "random": true }, "background.sidebar": { }, "background.panel": { }, "update.showReleaseNotes": false, "background.enabled": true, "excalidraw.theme": "auto", "explorer.confirmDragAndDrop": false, "workbench.browser.showInTitleBar": true, "workbench.editor.empty.hint": "hidden", "code-runner.runInTerminal": true, "code-runner.executorMap": { // 使用 python3 运行 Python 文件 "python": "python3 -u", // 使用 g++ 编译并运行 C++ 文件 "cpp": "cd `$dir && g++ -Wall -Wextra -O2 `$fileName -o `$fileNameWithoutExt && `$dir`$fileNameWithoutExt", // 使用 node 运行 JavaScript 文件 "javascript": "node", // 使用 dotnet run 运行 C# 项目 "csharp": "cd `$dir && dotnet run" } } "@ $argv = @" { "locale": "zh-cn", "enable-crash-reporter": true, "crash-reporter-id": "$([guid]::NewGuid().ToString())" } "@ $argvPath = "$env:USERPROFILE\.vscode\argv.json" try{ $argv | Set-Content -Path $argvPath } catch{ Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red } $targetDir = "$env:APPDATA\Code\User" $target = "$targetDir\settings.json" # 如果目标目录不存在则创建 if (-not (Test-Path $targetDir)) { New-Item -ItemType Directory -Path $targetDir | Out-Null } try{ $content | Set-Content -Path $target -Encoding UTF8 Write-Host "VSCode 安装完成并成功应用自定义设置" -ForegroundColor Green } catch{ Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red } Write-Host "`n--- 脚本执行完毕 ---" -ForegroundColor Cyan pause ``` + D ```ps1 # ============================================= # 定义所需文件及路径 # ============================================= # MinGW 安装目录 $MinGWTargetDir = "D:\mingw" $MinGWBinDir = Join-Path -Path $MinGWTargetDir -ChildPath "bin" # VS Code 可执行文件目录(用于安装扩展,请确认 code.cmd 所在路径) $VSCodeBinDir = "D:\Programs\Microsoft VS Code\bin" # 扩展文件列表 $files = @( "ms-vscode.cpptools-1.31.4-win32-x64.vsix", "ms-ceintl.vscode-language-pack-zh-hans-1.110.2026041514.vsix", "pomdtr.excalidraw-editor-3.9.1.vsix", "shalldie.background-2.0.10.vsix", "formulahendry.code-runner-0.12.2.vsix", "yltx.vscode-luogu-4.14.0.vsix" ) # 字体文件 $fonts = @( "MapleMono-NF-CN-Medium.ttf", "FiraCode-Regular.ttf" ) # ============================================= # 1. 添加 MinGW 到系统环境变量 Path # ============================================= Write-Host "--- 1. 添加 MinGW 到环境变量 ---" -ForegroundColor Cyan if (-not (Test-Path $MinGWBinDir -PathType Container)) { Write-Host "警告:MinGW 可执行目录 $MinGWBinDir 不存在,请确认路径正确。" -ForegroundColor Yellow } else { $CurrentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") if ($CurrentPath -clike "*$MinGWBinDir*") { Write-Host "路径 $MinGWBinDir 已存在于系统 Path 中。" -ForegroundColor Yellow } else { Write-Host "正在添加 $MinGWBinDir 到系统 Path ..." -ForegroundColor Yellow $NewPath = "$CurrentPath;$MinGWBinDir" [Environment]::SetEnvironmentVariable("Path", $NewPath, "Machine") Write-Host "Path 更新完成。新会话生效。" -ForegroundColor Green } } # ============================================= # 2. 安装多个字体(使用 Shell.Application) # ============================================= Write-Host "`n--- 2. 安装字体 ---" -ForegroundColor Cyan # 加载 Shell.Application 用于安装字体 $shell = New-Object -ComObject Shell.Application $fontsFolder = $shell.NameSpace(0x14) # 0x14 = Fonts 文件夹 foreach ($fontFile in $fonts) { $fullPath = Join-Path $PSScriptRoot $fontFile # 使用绝对路径更可靠 if (-not (Test-Path $fullPath)) { Write-Host "警告:字体文件 $fontFile 不存在,跳过。" -ForegroundColor Yellow continue } try { # CopyHere 会自动完成复制+注册,无需手动复制 $fontsFolder.CopyHere($fullPath, 0x10) # 0x10 = 覆盖已存在文件 Write-Host "已安装字体:$fontFile" -ForegroundColor Green } catch { Write-Host "安装字体失败:$($_.Exception.Message)" -ForegroundColor Red } } # 发送字体更改通知(让正在运行的程序刷新字体列表) Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; public class FontUtil { [DllImport("user32.dll", SetLastError=true)] public static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); } "@ $HWND_BROADCAST = [intptr]0xffff $WM_FONTCHANGE = 0x001D [FontUtil]::SendMessage($HWND_BROADCAST, $WM_FONTCHANGE, [intptr]0, [intptr]0) Write-Host "字体已安装并刷新系统。" -ForegroundColor Green # ============================================= # 3. 安装 VS Code 扩展 # ============================================= Write-Host "`n--- 3. 安装 VS Code 扩展 ---" -ForegroundColor Cyan # 检查 code 命令是否可用 $codeCmd = Join-Path -Path $VSCodeBinDir -ChildPath "code.cmd" if (-not (Test-Path $codeCmd)) { Write-Host "错误:未找到 code.cmd(路径:$codeCmd),请修改 `$VSCodeBinDir 变量。" -ForegroundColor Red } else { foreach ($f in $files) { if (Test-Path $f) { Write-Host "安装扩展:$f" -ForegroundColor Yellow & $codeCmd --install-extension $f } else { Write-Host "警告:扩展文件 $f 不存在,跳过。" -ForegroundColor Yellow } } # 关闭已打开的 VS Code,避免扩展加载冲突 try { Get-Process -Name Code | Stop-Process -Force Write-Host "已关闭正在运行的 VS Code。" -ForegroundColor Gray } catch {} } # ============================================= # 4. 写入 VS Code 配置文件 (settings.json & argv.json) # ============================================= Write-Host "`n--- 4. 配置 VS Code 设置 ---" -ForegroundColor Cyan $content = @" { "editor.smoothScrolling": true, "editor.cursorBlinking": "expand", "editor.guides.bracketPairs": true, "editor.bracketPairColorization.enabled": true, "editor.acceptSuggestionOnEnter": "smart", "editor.suggestSelection": "recentlyUsedByPrefix", "editor.autoClosingDelete": "always", "editor.autoClosingOvertype": "always", "editor.fontSize": 18, "editor.fontFamily": "'Maple Mono NF CN Medium','Fira Code', monospace", "editor.fontLigatures": true, "editor.formatOnSave": true, "extensions.ignoreRecommendations": true, "editor.mouseWheelZoom": true, "editor.cursorSmoothCaretAnimation": "on", "files.autoGuessEncoding": true, "C_Cpp.clang_format_fallbackStyle": "{ColumnLimit: 70, BasedOnStyle: LLVM, IndentWidth: 4, TabWidth: 4 }", "workbench.startupEditor": "none", "security.workspace.trust.untrustedFiles": "open", "C_Cpp.default.compilerPath": "D:/mingw/bin/g++.exe", "files.associations": { "*.cpp": "cpp" }, "[cpp]": { "editor.wordBasedSuggestions": "off", "editor.semanticHighlighting.enabled": true, "editor.stickyScroll.defaultModel": "foldingProviderModel", "editor.suggest.insertMode": "replace" }, "explorer.confirmDelete": false, "json.format.keepLines": true, "background.fullscreen": { "images": [ // "https://upload-bbs.miyoushe.com/upload/2026/04/26/404580670/f9699cd0b02044a5c167b1efe554a088_6490500312537179773.jpg", // "https://cdn.luogu.com.cn/upload/image_hosting/aiu0h201.png", // "https://upload-bbs.miyoushe.com/upload/2024/01/18/311370925/de6edf6312b5c2ee4e0d14f70ba6234e_9009544746652854059.jpg" "https://album.biliimg.com/bfs/new_dyn/4256c9b037af54280d186ef08a23e55f1349174610.jpg" ], "opacity": 0.3, "size": "cover", "position": "center", "interval": 60, "random": true }, "background.sidebar": { }, "background.panel": { }, "update.showReleaseNotes": false, "background.enabled": true, "excalidraw.theme": "auto", "explorer.confirmDragAndDrop": false, "workbench.browser.showInTitleBar": true, "workbench.editor.empty.hint": "hidden", "code-runner.runInTerminal": true, "code-runner.executorMap": { "python": "python3 -u", "cpp": "cd `$dir && g++ -Wall -Wextra -O2 `$fileName -o `$fileNameWithoutExt && `$dir`$fileNameWithoutExt", "javascript": "node", "csharp": "cd `$dir && dotnet run" } } "@ $argv = @" { "locale": "zh-cn", "enable-crash-reporter": true, "crash-reporter-id": "$([guid]::NewGuid().ToString())" } "@ $argvPath = "$env:USERPROFILE\.vscode\argv.json" try{ $argv | Set-Content -Path $argvPath } catch{ Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red } $targetDir = "$env:APPDATA\Code\User" $target = "$targetDir\settings.json" # 如果目标目录不存在则创建 if (-not (Test-Path $targetDir)) { New-Item -ItemType Directory -Path $targetDir | Out-Null } try{ $content | Set-Content -Path $target -Encoding UTF8 Write-Host "VSCode 安装完成并成功应用自定义设置" -ForegroundColor Green } catch{ Write-Host "错误详情:$($_.Exception.Message)" -ForegroundColor Red } Write-Host "`n--- 脚本执行完毕 ---" -ForegroundColor Cyan pause ```