electron-vue-exam-single/electron/db/path.js
chenqiang 7e8c417c48 p
2025-08-12 11:39:54 +08:00

42 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { app } from 'electron';
import path from 'path';
import fs from 'fs';
// 检测是否为便携模式
let isPortable = false;
const exePath = app.getPath('exe');
const appDir = path.dirname(exePath);
const portableFlagPath = path.join(appDir, 'portable.txt');
// 如果应用目录中存在portable.txt文件则启用便携模式
if (fs.existsSync(portableFlagPath)) {
isPortable = true;
console.log('启用便携模式');
}
// 根据模式选择数据目录
let dataDir;
if (isPortable) {
// 便携模式数据存储在应用目录下的data文件夹
dataDir = path.join(appDir, 'data');
} else {
// 非便携模式:数据存储在用户数据目录
const userDataPath = app.getPath('userData');
dataDir = path.join(userDataPath, 'data');
}
// 确保数据目录存在
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
console.log(`创建数据目录: ${dataDir}`);
}
// 系统数据库路径
export function getSystemDbPath() {
return path.join(dataDir, 'system.db');
}
// 用户数据库路径
export function getUserDbPath() {
return path.join(dataDir, 'user.db');
}