如何在執行C# .net8應用程式若發現沒有RUNTIME環境 自動安裝
如何在執行C# .net8應用程式若發現沒有RUNTIME環境 自動安裝
資料來源: copilot
方法一:使用安装程序(Installer)
方法二:發布為(Self-contained)應用程序
方法三:使用自定義啟動程序(Bootstrapper): 建議 C++、Go 或 Rust
net8_Bootstrapper.bat [BAT+WGET 版]
@echo off
setlocal enabledelayedexpansion
:: 檢查是否安裝了 WindowsDesktop 8.X 運行時
:: 獲取運行時列表並存儲在變數中
set "runtimeList="
set "foundRuntime=0"
for /f "delims=" %%i in ('dotnet --list-runtimes') do (
set "runtimeList=!runtimeList!%%i\n"
REM echo 檢查運行時: %%i
:: 檢查每一行是否包含 Microsoft.WindowsDesktop.App 8.
REM 當使用 /r 選項時,findstr 可以處理正則表達式,這讓你可以進行更複雜的模式匹配
REM 當使用 /c 選項時,你可以指定一個精確的字符串來搜索(字串比對)。這個字符串會被當作一個整體來進行匹配
echo %%i | findstr /r /c:"Microsoft\.WindowsDesktop\.App 8\.[0-9]" >nul
if !errorlevel! == 0 (
set "foundRuntime=1"
echo 匹配到的運行時: %%i
)
)
:: 列印 runtimeList 變數內容
REM echo !runtimeList!
:: 查找特定的運行時
if !foundRuntime! == 1 (
echo 已檢測到 WindowsDesktop 8.X 運行時。
) else (
echo 未檢測到 WindowsDesktop 8.X 運行時,正在安裝...
:: 靜默安裝運行時
if exist "windowsdesktop-runtime-8.0.12-win-x64.exe" (
echo 找到安裝檔。
) else (
echo 正在下載安裝檔。
wget --no-check-certificate https://download.visualstudio.microsoft.com/download/pr/f1e7ffc8-c278-4339-b460-517420724524/f36bb75b2e86a52338c4d3a90f8dac9b/windowsdesktop-runtime-8.0.12-win-x64.exe
)
echo 運行安裝。
start /wait windowsdesktop-runtime-8.0.12-win-x64.exe /quiet /norestart
echo 運行時安裝完成。
del windowsdesktop-runtime-8.0.12-win-x64.exe
)
echo 腳本執行完畢。
endlocal
start "" "vpos.exe"
:: 立即關閉批次腳本
exit
One thought on “如何在執行C# .net8應用程式若發現沒有RUNTIME環境 自動安裝”
chatgpt 提供偵測BAT
@echo off
setlocal
:: 执行 dotnet --list-runtimes 并检查输出
for /f "tokens=*" %%i in ('dotnet --list-runtimes') do (
REM 當使用 /r 選項時,findstr 可以處理正則表達式,這讓你可以進行更複雜的模式匹配
REM 當使用 /c 選項時,你可以指定一個精確的字符串(字串比對/字串比較)來搜索。這個字符串會被當作一個整體來進行匹配
echo %%i | findstr /r /c:"Microsoft\.WindowsDesktop\.App 8\.[0-9]" >nul
if %errorlevel% equ 0 (
echo .NET 8 Runtime is installed.
pause
goto :eof
)
)
echo .NET 8 Runtime is not installed.
endlocal
pause