Tauri 在Rust中访问文件
这是基于上面的示例。在Rust方面,您需要一个PathResolver
的实例,您可以从App
和AppHandle
中获取:
tauri::Builder::default()
.setup(|app| {
let resource_path = app.path_resolver()
.resolve_resource("lang/de.json")
.expect("failed to resolve resource");
let file = std::fs::File::open(&resource_path).unwrap();
let lang_de: serde_json::Value = serde_json::from_reader(file).unwrap();
println!("{}", lang_de.get("hello").unwrap()); // This will print 'Guten Tag!' to the terminal
Ok(())
})
#[tauri::command]
fn hello(handle: tauri::AppHandle) -> String {
let resource_path = handle.path_resolver()
.resolve_resource("lang/de.json")
.expect("failed to resolve resource");
let file = std::fs::File::open(&resource_path).unwrap();
let lang_de: serde_json::Value = serde_json::from_reader(file).unwrap();
lang_de.get("hello").unwrap()
}