You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Tauri应用内获取tauri.conf.json中的bundleIdentifier值?

获取Tauri配置中bundle identifier的正确方式

你可以通过Tauri内置的app模块动态获取配置里的bundle identifier,不用硬编码字符串,具体实现如下:

  • 引入window.__TAURI__.app模块
  • 调用异步方法app.getAppInfo(),返回的对象里包含bundle.identifier字段,这就是你要的配置值
  • 用这个动态获取的值替换原来的硬编码id

修改后的完整代码:

const fs = window.__TAURI__.fs;
const path = window.__TAURI__.path;
const app = window.__TAURI__.app;

// 动态获取bundle identifier
const appInfo = await app.getAppInfo();
const id = appInfo.bundle.identifier;

let filename = "my-file.txt";
let _path = await path.join(id, filename);

let data = "Hello World!";

await fs.writeFile({
    path: _path,
    contents: data
}, { dir: fs.Dir.Data });

说明:getAppInfo()是异步方法,必须用await调用才能拿到正确结果。后续如果修改tauri.conf.json里的identifier值,代码会自动同步更新,无需手动修改字符串。

内容的提问来源于stack exchange,提问作者a_random_username_for_me_01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:42:10