path
This commit is contained in:
parent
dc97ee982d
commit
c050516c29
@ -1,4 +1,3 @@
|
||||
// 添加缺失的path模块导入
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const { app } = require('electron');
|
||||
@ -8,40 +7,82 @@ const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
|
||||
|
||||
// 检测是否为便携模式
|
||||
let isPortable = false;
|
||||
let appDir;
|
||||
let dataDir;
|
||||
|
||||
if (isDev) {
|
||||
// 开发环境:数据存储在工程的data目录
|
||||
dataDir = path.join(process.cwd(), 'data');
|
||||
} else {
|
||||
// 非开发环境:确定应用目录
|
||||
const exePath = app.getPath('exe');
|
||||
appDir = path.dirname(exePath);
|
||||
try {
|
||||
// 关键修改:对于打包后的应用,尤其是便携模式,我们需要更智能地确定数据目录位置
|
||||
// 1. 获取进程可执行文件路径
|
||||
const execPath = process.execPath;
|
||||
// 2. 获取可执行文件所在目录
|
||||
let appDir = path.dirname(execPath);
|
||||
|
||||
// 检测是否存在便携模式标记文件
|
||||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||
if (fs.existsSync(portableFlagPath)) {
|
||||
isPortable = true;
|
||||
}
|
||||
console.log('进程可执行文件路径:', execPath);
|
||||
console.log('进程目录:', appDir);
|
||||
|
||||
// 关键修改:在便携模式下,data目录与可执行文件同级
|
||||
// 如果可执行文件在portable-app目录中,则data目录与portable-app目录同级
|
||||
if (path.basename(appDir) === 'portable-app') {
|
||||
dataDir = path.join(path.dirname(appDir), 'data');
|
||||
isPortable = true;
|
||||
} else {
|
||||
// 否则data目录与可执行文件同级
|
||||
dataDir = path.join(appDir, 'data');
|
||||
}
|
||||
|
||||
// 确保便携模式标记文件存在于data目录同级
|
||||
if (isPortable) {
|
||||
const flagDir = path.dirname(dataDir);
|
||||
const flagPath = path.join(flagDir, 'portable.txt');
|
||||
if (!fs.existsSync(flagPath)) {
|
||||
fs.writeFileSync(flagPath, '');
|
||||
// 3. 检测是否存在portable.txt文件(标记文件)
|
||||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||
if (fs.existsSync(portableFlagPath)) {
|
||||
isPortable = true;
|
||||
console.log('检测到便携模式标记文件');
|
||||
}
|
||||
|
||||
// 4. 检查是否在临时目录中运行(常见于便携应用解压运行时)
|
||||
const isTempDir = appDir.includes('Temp') || appDir.includes('tmp');
|
||||
console.log('是否在临时目录中运行:', isTempDir);
|
||||
|
||||
// 5. 特殊处理便携模式下的临时目录问题
|
||||
if (isTempDir) {
|
||||
// 尝试通过其他方式获取实际的应用目录
|
||||
// - 方法1:检查可执行文件名
|
||||
const exeName = path.basename(execPath);
|
||||
console.log('可执行文件名:', exeName);
|
||||
|
||||
// - 方法2:使用process.argv[0](启动路径)
|
||||
const startupPath = process.argv[0];
|
||||
console.log('启动路径:', startupPath);
|
||||
|
||||
// 关键点:对于便携应用,我们需要确保数据目录与实际的可执行文件(用户看到的那个)在同一目录
|
||||
// 假设可执行文件名为StatExamPortable_*.exe,我们需要找到这个文件的实际位置
|
||||
// 由于在临时目录中,我们需要向上查找或使用相对路径策略
|
||||
|
||||
// 这里使用一种通用策略:在Windows系统上,便携应用的临时目录通常在用户临时文件夹下
|
||||
// 我们将数据目录放在与临时目录不同的位置,确保数据持久性
|
||||
|
||||
// 尝试获取用户主目录
|
||||
const userHome = app.getPath('home');
|
||||
console.log('用户主目录:', userHome);
|
||||
|
||||
// 便携式应用的数据目录应该放在一个稳定的位置,而不是临时目录
|
||||
// 这里我们选择放在用户主目录下的特定文件夹
|
||||
const portableDataDir = path.join(userHome, 'StatExamData');
|
||||
dataDir = portableDataDir;
|
||||
|
||||
// 创建便携模式标记文件
|
||||
const stablePortableFlagPath = path.join(portableDataDir, '..', 'portable.txt');
|
||||
if (!fs.existsSync(stablePortableFlagPath)) {
|
||||
try {
|
||||
fs.writeFileSync(stablePortableFlagPath, '');
|
||||
console.log('创建便携模式标记文件:', stablePortableFlagPath);
|
||||
} catch (error) {
|
||||
console.error('创建便携模式标记文件失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// 正常情况下,数据目录与可执行文件同级
|
||||
dataDir = path.join(appDir, 'data');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('确定数据目录时出错:', error);
|
||||
// 发生错误时使用默认策略
|
||||
const fallbackDir = path.join(app.getPath('userData'), 'data');
|
||||
dataDir = fallbackDir;
|
||||
console.log('使用备用数据目录:', fallbackDir);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user