作業系統資訊
使用作業系統資訊外掛程式讀取關於作業系統的資訊。
支援的平台
此外掛程式需要 Rust 版本至少 1.77.2
平台 | 等級 | 備註 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
設定
安裝作業系統資訊外掛程式以開始使用。
使用您專案的套件管理器新增依賴項
npm run tauri add os
yarn run tauri add os
pnpm tauri add os
deno task tauri add os
bun tauri add os
cargo tauri add os
-
在
src-tauri
資料夾中執行以下指令,將此外掛程式新增至Cargo.toml
專案的依賴項中cargo add tauri-plugin-os -
修改
lib.rs
以初始化此外掛程式src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_os::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
如果您想要在 JavaScript 中使用,也請安裝 npm 套件
npm install @tauri-apps/plugin-osyarn add @tauri-apps/plugin-ospnpm add @tauri-apps/plugin-osdeno add npm:@tauri-apps/plugin-osbun add @tauri-apps/plugin-os
使用方式
透過此外掛程式,您可以查詢目前作業系統的多種資訊。請參閱 JavaScript API 或 Rust API 參考文件中的所有可用函數。
範例:作業系統平台
platform
傳回描述所使用特定作業系統的字串。此值在編譯時設定。可能的值為 linux
、macos
、ios
、freebsd
、dragonfly
、netbsd
、openbsd
、solaris
、android
、windows
。
import { platform } from '@tauri-apps/plugin-os';// when using `"withGlobalTauri": true`, you may use// const { platform } = window.__TAURI__.os;
const currentPlatform = platform();console.log(currentPlatform);// Prints "windows" to the console
let platform = tauri_plugin_os::platform();println!("Platform: {}", platform);// Prints "windows" to the terminal
權限
預設情況下,所有潛在危險的外掛程式指令和範圍都被封鎖,無法存取。您必須修改 capabilities
設定中的權限才能啟用這些。
請參閱功能總覽以取得更多資訊,以及逐步指南以使用外掛程式權限。
{ "permissions": [ ..., "os:default" ]}
預設權限
此權限設定配置可以從前端收集哪些作業系統資訊。
已授與的權限
除了主機名稱之外的所有資訊都可用。
allow-arch
allow-exe-extension
allow-family
allow-locale
allow-os-type
allow-platform
allow-version
權限表
識別碼 | 描述 |
---|---|
|
啟用 arch 指令,而無需任何預先設定的範圍。 |
|
拒絕 arch 指令,而無需任何預先設定的範圍。 |
|
啟用 exe_extension 指令,而無需任何預先設定的範圍。 |
|
拒絕 exe_extension 指令,而無需任何預先設定的範圍。 |
|
啟用 family 指令,而無需任何預先設定的範圍。 |
|
拒絕 family 指令,而無需任何預先設定的範圍。 |
|
啟用 hostname 指令,而無需任何預先設定的範圍。 |
|
拒絕 hostname 指令,而無需任何預先設定的範圍。 |
|
啟用 locale 指令,而無需任何預先設定的範圍。 |
|
拒絕 locale 指令,而無需任何預先設定的範圍。 |
|
啟用 os_type 指令,而無需任何預先設定的範圍。 |
|
拒絕 os_type 指令,而無需任何預先設定的範圍。 |
|
啟用 platform 指令,而無需任何預先設定的範圍。 |
|
拒絕 platform 指令,而無需任何預先設定的範圍。 |
|
啟用 version 指令,而無需任何預先設定的範圍。 |
|
拒絕 version 指令,而無需任何預先設定的範圍。 |
© 2025 Tauri Contributors。CC-BY / MIT