操作系统信息
使用操作系统信息插件读取操作系统信息。
Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
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';// 当设置 `"withGlobalTauri": true` 时,你可以用// const { platform } = window.__TAURI__.os;
const currentPlatform = platform();console.log(currentPlatform);// 将 "windows" 输出到控制台
let platform = tauri_plugin_os::platform();println!("Platform: {}", platform);// 将 "windows" 输出到终端
权限
默认情况下,所有具有潜在危险的插件命令和范围都会被阻止且无法访问。您必须修改 capabilities
文件夹中的配置来启用它们。
参见能力概览以获取更多信息,以及插件的分步导览来调整插件权限。
{ "permissions": [ ..., "os:default" ]}
Default Permission
This permission set configures which operating system information are available to gather from the frontend.
Granted Permissions
All information except the host name are available.
This default permission set includes the following:
allow-arch
allow-exe-extension
allow-family
allow-locale
allow-os-type
allow-platform
allow-version
Permission Table
Identifier | Description |
---|---|
|
Enables the arch command without any pre-configured scope. |
|
Denies the arch command without any pre-configured scope. |
|
Enables the exe_extension command without any pre-configured scope. |
|
Denies the exe_extension command without any pre-configured scope. |
|
Enables the family command without any pre-configured scope. |
|
Denies the family command without any pre-configured scope. |
|
Enables the hostname command without any pre-configured scope. |
|
Denies the hostname command without any pre-configured scope. |
|
Enables the locale command without any pre-configured scope. |
|
Denies the locale command without any pre-configured scope. |
|
Enables the os_type command without any pre-configured scope. |
|
Denies the os_type command without any pre-configured scope. |
|
Enables the platform command without any pre-configured scope. |
|
Denies the platform command without any pre-configured scope. |
|
Enables the version command without any pre-configured scope. |
|
Denies the version command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT