diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 72ffa81a38..b5424c81ab 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -3,7 +3,7 @@ import { arch } from 'node:os' import path from 'node:path' import { loggerService } from '@logger' -import { isLinux, isMac, isWin } from '@main/constant' +import { isLinux, isMac, isPortable, isWin } from '@main/constant' import { getBinaryPath, isBinaryExists, runInstallScript } from '@main/utils/process' import { handleZoomFactor } from '@main/utils/zoom' import { UpgradeChannel } from '@shared/config/constant' @@ -335,6 +335,12 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { options.args.unshift('--appimage-extract-and-run') } + if (isWin && isPortable) { + options = options || {} + options.execPath = process.env.PORTABLE_EXECUTABLE_FILE + options.args = options.args || [] + } + app.relaunch(options) app.exit(0) }) diff --git a/src/main/utils/file.ts b/src/main/utils/file.ts index 4f67fd6504..19d37b28f6 100644 --- a/src/main/utils/file.ts +++ b/src/main/utils/file.ts @@ -4,7 +4,7 @@ import os from 'node:os' import path from 'node:path' import { loggerService } from '@logger' -import { isLinux, isPortable } from '@main/constant' +import { isLinux, isPortable, isWin } from '@main/constant' import { audioExts, documentExts, imageExts, MB, textExts, videoExts } from '@shared/config/constant' import { FileMetadata, FileTypes } from '@types' import { app } from 'electron' @@ -72,6 +72,10 @@ function getAppDataPathFromConfig() { executablePath = path.join(path.dirname(process.env.APPIMAGE), 'cherry-studio.appimage') } + if (isWin && isPortable) { + executablePath = path.join(process.env.PORTABLE_EXECUTABLE_DIR || '', 'cherry-studio-portable.exe') + } + let appDataPath = null // 兼容旧版本 if (config.appDataPath && typeof config.appDataPath === 'string') { @@ -108,6 +112,11 @@ export function updateAppDataConfig(appDataPath: string) { executablePath = path.join(path.dirname(process.env.APPIMAGE), 'cherry-studio.appimage') } + // 如果是 Windows 可移植版本,则使用 PORTABLE_EXECUTABLE_FILE 环境变量 + if (isWin && isPortable) { + executablePath = path.join(process.env.PORTABLE_EXECUTABLE_DIR || '', 'cherry-studio-portable.exe') + } + if (!fs.existsSync(configPath)) { fs.writeFileSync(configPath, JSON.stringify({ appDataPath: [{ executablePath, dataPath: appDataPath }] }, null, 2)) return