electron 使用ipcMain.handle 和ipcMain.invoke

electron 使用ipcMain.handle 和ipcMain.invoke 發(fā)現(xiàn)中文路徑亂碼,同時(shí)下載了windows-shortcuts 進(jìn)行解析路徑,發(fā)現(xiàn)還是亂碼
解決辦法:1.將windows-shortcuts里面的Shortcut.exe文件復(fù)制到根目錄,同時(shí)在electron--main.ts下編寫代碼
/**

  • 解析 Shortcut.exe 的輸出結(jié)果
    */
    function parseQuery(stdout: string): {
    target?: string;
    args?: string;
    workingDir?: string;
    runStyle?: number;
    icon?: string;
    iconIndex?: string;
    hotkey?: number;
    desc?: string;
    expanded: {
    target?: string;
    args?: string;
    workingDir?: string;
    icon?: string;
    };
    } {
    const result: any = {
    expanded: {},
    };

const lines = stdout.split(/[\r\n]+/);

for (const line of lines) {
if (!line.includes('=')) continue;

const [key, value] = line.split('=', 2);

switch (key) {
  case 'TargetPath':
    result.target = value;
    break;
  case 'TargetPathExpanded':
    result.expanded.target = value;
    break;
  case 'Arguments':
    result.args = value;
    break;
  case 'ArgumentsExpanded':
    result.expanded.args = value;
    break;
  case 'WorkingDirectory':
    result.workingDir = value;
    break;
  case 'WorkingDirectoryExpanded':
    result.expanded.workingDir = value;
    break;
  case 'RunStyle':
    result.runStyle = parseInt(value);
    break;
  case 'IconLocation': {
    const [iconPath, iconIndex] = value.split(',');
    result.icon = iconPath;
    result.iconIndex = iconIndex;
    break;
  }
  case 'IconLocationExpanded': {
    const [iconPath] = value.split(',');
    result.expanded.icon = iconPath;
    break;
  }
  case 'HotKey': {
    const match = value.match(/\d+/);
    if (match) result.hotkey = parseInt(match[0]);
    break;
  }
  case 'Description':
    result.desc = value;
    break;
}

}

// fallback 邏輯:如果 expanded 屬性缺失,補(bǔ)全它
for (const key of ['target', 'args', 'workingDir', 'icon']) {
if (!result.expanded[key]) {
result.expanded[key] = result[key];
}
}

return result;
}

/**

  • 處理解析 lnk 文件的請(qǐng)求
    */
    ipcMain.handle('resolve-lnk', async (_event, lnkPath: string) => {
    const shortcutExePath = isDev
    ? path.join(__dirname, '../native/Shortcut.exe') // 開發(fā)環(huán)境路徑
    : path.join(process.resourcesPath, 'native/Shortcut.exe'); // 打包后路徑
    // 直接使用 lnkPath,不展開環(huán)境變量
    const args = ['/A:Q', /F:${lnkPath}];

return new Promise((resolve, reject) => {
execFile(shortcutExePath, args, { encoding: 'buffer' }, (error, stdout, stderr) => {
if (error) {
console.error('[Shortcut.exe Error]', error);
reject(stderr || stdout || error.message);
return;
}
try {
const output = iconv.decode(stdout, 'gbk');
console.log('[Shortcut Output]', output);
const result = parseQuery(output);
resolve(result);
} catch (e: any) {
reject('解析失敗: ' + e.message);
}
});
});
});

輸出的就是中文的,需要打開exe文件的
function executeExe(exePath: string): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
const result = await shell.openPath(exePath);
if (result) {
reject(執(zhí)行失敗: ${result});
} else {
resolve('啟動(dòng)成功');
}
} catch (err: any) {
reject(執(zhí)行出錯(cuò): ${err.message});
}
});
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容