文件系统
访问文件系统。
此插件需要 Rust 版本至少为 **1.77.2**
| 平台 | 级别 | 备注 |
|---|---|---|
| Windows | 通过 MSI 或 NSIS 在 | |
| Linux | 无权写入 | |
| macOS | 无权写入 | |
| Android | | 默认情况下,访问权限仅限于应用程序文件夹 |
| iOS | | 默认情况下,访问权限仅限于应用程序文件夹 |
安装 fs 插件即可开始使用。
使用你的项目包管理器添加依赖项
npm run tauri add fsyarn run tauri add fspnpm tauri add fsdeno task tauri add fsbun tauri add fscargo tauri add fs-
在
src-tauri文件夹中运行以下命令,将插件添加到项目的Cargo.toml依赖项中cargo add tauri-plugin-fs -
修改
lib.rs以初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_fs::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用您首选的 JavaScript 包管理器安装 JavaScript 访客绑定
npm install @tauri-apps/plugin-fsyarn add @tauri-apps/plugin-fspnpm add @tauri-apps/plugin-fsdeno add npm:@tauri-apps/plugin-fsbun add @tauri-apps/plugin-fs
当使用 audio、cache、documents、downloads、picture、public 或 video 目录时,您的应用必须有权访问外部存储。
将以下权限添加到 gen/android/app/src/main/AndroidManifest.xml 文件中的 manifest 标签:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Apple 要求应用程序开发者指定 API 使用的批准理由,以增强用户隐私。
您必须在 src-tauri/gen/apple 文件夹中创建一个 PrivacyInfo.xcprivacy 文件,其中包含必需的 NSPrivacyAccessedAPICategoryFileTimestamp 键和 C617.1 推荐理由。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"> <dict> <key>NSPrivacyAccessedAPITypes</key> <array> <dict> <key>NSPrivacyAccessedAPIType</key> <string>NSPrivacyAccessedAPICategoryFileTimestamp</string> <key>NSPrivacyAccessedAPITypeReasons</key> <array> <string>C617.1</string> </array> </dict> </array> </dict></plist>fs 插件在 JavaScript 和 Rust 中均可用。
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';// when using `"withGlobalTauri": true`, you may use// const { exists, BaseDirectory } = window.__TAURI__.fs;
// Check if the `$APPDATA/avatar.png` file existsawait exists('avatar.png', { baseDir: BaseDirectory.AppData });use tauri_plugin_fs::FsExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_fs::init()) .setup(|app| { // allowed the given directory let scope = app.fs_scope(); scope.allow_directory("/path/to/directory", false); dbg!(scope.allowed());
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}此模块可防止路径遍历,不允许使用父目录访问器(即不允许使用“/usr/path/to/../file”或“../path/to/file”路径)。通过此 API 访问的路径必须相对于 base directories 之一,或者通过 path API 创建。
有关更多信息,请参阅 @tauri-apps/plugin-fs - Security。
文件系统插件提供了两种操作路径的方式:基本目录和 路径 API。
-
基本目录
每个 API 都有一个 options 参数,允许您定义一个 baseDir,作为操作的工作目录。
import { readFile } from '@tauri-apps/plugin-fs';const contents = await readFile('avatars/tauri.png', {baseDir: BaseDirectory.Home,});在上面的示例中,由于我们使用的是 **Home** 基本目录,因此会读取 ~/avatars/tauri.png 文件。
-
路径 API
或者,您可以使用路径 API 执行路径操作。
import { readFile } from '@tauri-apps/plugin-fs';import * as path from '@tauri-apps/api/path';const home = await path.homeDir();const contents = await readFile(await path.join(home, 'avatars/tauri.png'));
创建文件并返回其句柄。如果文件已存在,则会将其截断。
import { create, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await create('foo/bar.txt', { baseDir: BaseDirectory.AppData });await file.write(new TextEncoder().encode('Hello world'));await file.close();为提高性能,插件提供了单独的 API 用于写入文本文件和二进制文件。
-
文本文件
import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = JSON.stringify({ notifications: true });await writeTextFile('config.json', contents, {baseDir: BaseDirectory.AppConfig,}); -
二进制文件
import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = new Uint8Array(); // fill a byte arrayawait writeFile('config', contents, {baseDir: BaseDirectory.AppConfig,});
打开文件并返回其句柄。通过此 API,您可以更好地控制文件的打开方式(只读模式、只写模式、追加而非覆盖、仅在文件不存在时创建等)。
-
只读
这是默认模式。
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {read: true,baseDir: BaseDirectory.AppData,});const stat = await file.stat();const buf = new Uint8Array(stat.size);await file.read(buf);const textContents = new TextDecoder().decode(buf);await file.close(); -
只写
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('Hello world'));await file.close();默认情况下,文件在任何
file.write()调用时都会被截断。请参阅以下示例,了解如何追加到现有内容。 -
追加
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {append: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();请注意,
{ append: true }的效果与{ write: true, append: true }相同。 -
截断
当设置了
truncate选项且文件已存在时,它将被截断为长度 0。import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,truncate: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();此选项要求
write设置为true。如果想通过多次
file.write()调用来重写现有文件,可以将其与append选项一起使用。 -
创建
默认情况下,
openAPI 仅打开现有文件。要创建文件(如果不存在)并打开它(如果存在),请将create设置为true。import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,create: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();要创建文件,
write或append也必须设置为true。如果文件已存在则失败,请参阅
createNew。 -
createNew
createNew的工作方式类似于create,但如果文件已存在,则会失败。import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,createNew: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();要创建文件,
write也必须设置为true。
为了提高性能,该插件提供了单独的 API 用于读取文本文件和二进制文件。
-
文本文件
import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';const configToml = await readTextFile('config.toml', {baseDir: BaseDirectory.AppConfig,});如果文件很大,您可以使用
readTextFileLinesAPI 流式读取其行import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';const lines = await readTextFileLines('app.logs', {baseDir: BaseDirectory.AppLog,});for await (const line of lines) {console.log(line);} -
二进制文件
import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';const icon = await readFile('icon.png', {baseDir: BaseDirectory.Resources,});
调用 remove() 删除文件。如果文件不存在,则返回错误。
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('user.db', { baseDir: BaseDirectory.AppLocalData });copyFile 函数接受源路径和目标路径。请注意,您必须单独配置每个基本目录。
import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';await copyFile('user.db', 'user.db.bk', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp,});在上面的示例中,
使用 exists() 函数检查文件是否存在
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';const tokenExists = await exists('token', { baseDir: BaseDirectory.AppLocalData,});文件元数据可以通过 stat 和 lstat 函数检索。stat 遵循符号链接(如果实际指向的文件不在范围内,则返回错误),而 lstat 不遵循符号链接,返回符号链接本身的信息。
import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';const metadata = await stat('app.db', { baseDir: BaseDirectory.AppLocalData,});rename 函数接受源路径和目标路径。请注意,您必须单独配置每个基本目录。
import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';await rename('user.db.bk', 'user.db', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp,});在上述示例中,<app-local-data>/user.db.bk 文件被重命名为 $TMPDIR/user.db。
将指定文件截断或扩展到指定的长度(默认为 0)。
- 截断为 0 长度
import { truncate } from '@tauri-apps/plugin-fs';await truncate('my_file.txt', 0, { baseDir: BaseDirectory.AppLocalData });- 截断到指定长度
import { truncate, readTextFile, writeTextFile, BaseDirectory,} from '@tauri-apps/plugin-fs';
const filePath = 'file.txt';await writeTextFile(filePath, 'Hello World', { baseDir: BaseDirectory.AppLocalData,});await truncate(filePath, 7, { baseDir: BaseDirectory.AppLocalData,});const data = await readTextFile(filePath, { baseDir: BaseDirectory.AppLocalData,});console.log(data); // "Hello W"要创建目录,请调用 mkdir 函数
import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';await mkdir('images', { baseDir: BaseDirectory.AppLocalData,});readDir 函数递归列出目录的条目
import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });调用 remove() 删除目录。如果目录不存在,则返回错误。
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('images', { baseDir: BaseDirectory.AppLocalData });如果目录不为空,则 recursive 选项必须设置为 true
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('images', { baseDir: BaseDirectory.AppLocalData, recursive: true,});使用 exists() 函数检查目录是否存在
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';const tokenExists = await exists('images', { baseDir: BaseDirectory.AppLocalData,});目录元数据可以通过 stat 和 lstat 函数检索。stat 遵循符号链接(如果实际指向的文件不在范围内,则返回错误),而 lstat 不遵循符号链接,返回符号链接本身的信息。
import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';const metadata = await stat('databases', { baseDir: BaseDirectory.AppLocalData,});要监视目录或文件的更改,请使用 watch 或 watchImmediate 函数。
-
监视
watch是防抖的,因此它只会在一定的延迟后发出事件import { watch, BaseDirectory } from '@tauri-apps/plugin-fs';await watch('app.log',(event) => {console.log('app.log event', event);},{baseDir: BaseDirectory.AppLog,delayMs: 500,}); -
watchImmediate
watchImmediate会立即通知监听器事件import { watchImmediate, BaseDirectory } from '@tauri-apps/plugin-fs';await watchImmediate('logs',(event) => {console.log('logs directory event', event);},{baseDir: BaseDirectory.AppLog,recursive: true,});
默认情况下,目录上的监视操作不是递归的。将 recursive 选项设置为 true 以递归监视所有子目录的更改。
默认情况下,所有潜在危险的插件命令和范围都被阻止,无法访问。您必须修改 capabilities 配置中的权限才能启用这些功能。
有关更多信息,请参阅功能概述,并参阅分步指南以使用插件权限。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ "fs:default", { "identifier": "fs:allow-exists", "allow": [{ "path": "$APPDATA/*" }] } ]}默认权限
这组权限描述了 fs 插件默认启用或拒绝的文件系统访问类型。
已授予权限
此默认权限集允许读取应用程序特定目录(AppConfig、AppData、AppLocalData、AppCache、AppLog)以及其中创建的所有文件和子目录。这些目录的位置取决于运行应用程序的操作系统。
通常,这些目录需要由应用程序在运行时手动创建,然后才能访问其中的文件或文件夹。
因此,也允许通过 mkdir 命令创建所有这些文件夹。
拒绝的权限
此默认权限集默认阻止对 Tauri 应用程序关键组件的访问。在 Windows 上,WebView 数据文件夹的访问被拒绝。
此默认权限集包括以下内容
create-app-specific-dirsread-app-specific-dirs-recursivedeny-default
权限表
| 标识符 | 描述 |
|---|---|
|
|
这允许对整个应用程序文件夹、文件和子目录进行完全递归读取访问。 |
|
|
这允许对整个应用程序文件夹、文件和子目录进行完全递归写入访问。 |
|
|
这允许对应用程序文件夹进行非递归读取访问。 |
|
|
这允许对应用程序文件夹进行非递归写入访问。 |
|
|
这允许对应用程序文件夹的元数据进行完全递归读取访问,包括文件列表和统计信息。 |
|
|
这允许对应用程序文件夹的元数据进行非递归读取访问,包括文件列表和统计信息。 |
|
|
此范围允许递归访问完整的应用程序文件夹,包括子目录和文件。 |
|
|
此范围允许访问应用程序文件夹中所有文件并列出顶级目录内容。 |
|
|
此范围允许列出应用程序目录中的所有文件和文件夹。 |
|
|
这允许对整个 |
|
|
这允许对整个 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对整个 |
|
|
这允许对整个 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
这允许对完整的 |
|
|
这允许对完整的 |
|
|
这允许对 |
|
|
这允许对 |
|
|
这允许对 |
|
|
此项权限允许非递归地读取 |
|
|
此范围允许递归访问完整的 |
|
|
此范围允许访问 |
|
|
此范围允许列出 |
|
|
启用 copy_file 命令,无需任何预配置范围。 |
|
|
禁用 copy_file 命令,无需任何预配置范围。 |
|
|
启用 create 命令,无需任何预配置范围。 |
|
|
禁用 create 命令,无需任何预配置范围。 |
|
|
启用 exists 命令,无需任何预配置范围。 |
|
|
禁用 exists 命令,无需任何预配置范围。 |
|
|
启用 fstat 命令,无需任何预配置范围。 |
|
|
禁用 fstat 命令,无需任何预配置范围。 |
|
|
启用 ftruncate 命令,无需任何预配置范围。 |
|
|
禁用 ftruncate 命令,无需任何预配置范围。 |
|
|
启用 lstat 命令,无需任何预配置范围。 |
|
|
禁用 lstat 命令,无需任何预配置范围。 |
|
|
启用 mkdir 命令,无需任何预配置范围。 |
|
|
禁用 mkdir 命令,无需任何预配置范围。 |
|
|
启用打开命令,不带任何预配置范围。 |
|
|
禁用打开命令,不带任何预配置范围。 |
|
|
启用 read 命令,无需任何预配置范围。 |
|
|
禁用 read 命令,无需任何预配置范围。 |
|
|
启用 read_dir 命令,无需任何预配置范围。 |
|
|
禁用 read_dir 命令,无需任何预配置范围。 |
|
|
启用 read_file 命令,无需任何预配置范围。 |
|
|
禁用 read_file 命令,无需任何预配置范围。 |
|
|
启用 read_text_file 命令,无需任何预配置范围。 |
|
|
禁用 read_text_file 命令,无需任何预配置范围。 |
|
|
启用 read_text_file_lines 命令,无需任何预配置范围。 |
|
|
禁用 read_text_file_lines 命令,无需任何预配置范围。 |
|
|
启用 read_text_file_lines_next 命令,无需任何预配置范围。 |
|
|
禁用 read_text_file_lines_next 命令,无需任何预配置范围。 |
|
|
启用 remove 命令,无需任何预配置范围。 |
|
|
禁用 remove 命令,无需任何预配置范围。 |
|
|
启用 rename 命令,无需任何预配置范围。 |
|
|
禁用 rename 命令,无需任何预配置范围。 |
|
|
启用 seek 命令,无需任何预配置范围。 |
|
|
禁用 seek 命令,无需任何预配置范围。 |
|
|
启用 size 命令,无需任何预配置范围。 |
|
|
禁用 size 命令,无需任何预配置范围。 |
|
|
启用 stat 命令,无需任何预配置范围。 |
|
|
禁用 stat 命令,无需任何预配置范围。 |
|
|
启用 truncate 命令,无需任何预配置范围。 |
|
|
禁用 truncate 命令,无需任何预配置范围。 |
|
|
启用 unwatch 命令,无需任何预配置范围。 |
|
|
禁用 unwatch 命令,无需任何预配置范围。 |
|
|
启用 watch 命令,无需任何预配置范围。 |
|
|
禁用 watch 命令,无需任何预配置范围。 |
|
|
启用 write 命令,无需任何预配置范围。 |
|
|
禁用 write 命令,无需任何预配置范围。 |
|
|
启用 write_file 命令,无需任何预配置范围。 |
|
|
禁用 write_file 命令,无需任何预配置范围。 |
|
|
启用 write_text_file 命令,无需任何预配置范围。 |
|
|
禁用 write_text_file 命令,无需任何预配置范围。 |
|
|
此权限允许创建应用程序特定目录。 |
|
|
此项默认拒绝访问危险的 Tauri 相关文件和文件夹。 |
|
|
此项拒绝在 Linux 上读取 |
|
|
此项拒绝在 Windows 上读取 |
|
|
此项启用所有与读取相关的命令,无需任何预配置的可访问路径。 |
|
|
此权限允许在应用程序特定基本目录上进行递归读取。 |
|
|
此项启用目录读取和文件元数据相关的命令,无需任何预配置的可访问路径。 |
|
|
此项启用文件读取相关的命令,无需任何预配置的可访问路径。 |
|
|
此项启用所有索引或元数据相关的命令,无需任何预配置的可访问路径。 |
|
|
您可以用来修改全局范围的空权限。 |
|
|
此项启用所有与写入相关的命令,无需任何预配置的可访问路径。 |
|
|
此项启用所有文件写入相关的命令,无需任何预配置的可访问路径。 |
此插件权限包括用于定义允许或明确拒绝的路径的范围。有关范围的更多信息,请参阅命令范围。
每个 allow 或 deny 范围必须包含一个列出所有应允许或拒绝的路径的数组。范围条目采用 { path: string } 格式。
范围条目可以使用 $<path> 变量来引用常见的系统路径,例如主目录、应用程序资源目录和配置目录。下表列出了所有可以引用的常见路径
| 路径 | 变量 |
|---|---|
| appConfigDir | $APPCONFIG |
| appDataDir | $APPDATA |
| appLocalDataDir | $APPLOCALDATA |
| appcacheDir | $APPCACHE |
| applogDir | $APPLOG |
| audioDir | $AUDIO |
| cacheDir | $CACHE |
| configDir | $CONFIG |
| dataDir | $DATA |
| localDataDir | $LOCALDATA |
| desktopDir | $DESKTOP |
| documentDir | $DOCUMENT |
| downloadDir | $DOWNLOAD |
| executableDir | $EXE |
| fontDir | $FONT |
| homeDir | $HOME |
| pictureDir | $PICTURE |
| publicDir | $PUBLIC |
| runtimeDir | $RUNTIME |
| templateDir | $TEMPLATE |
| videoDir | $VIDEO |
| resourceDir | $RESOURCE |
| tempDir | $TEMP |
- 全局范围
要将范围应用于任何 fs 命令,请使用 fs:scope 权限
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "fs:scope", "allow": [{ "path": "$APPDATA" }, { "path": "$APPDATA/**/*" }] } ]}要将范围应用于特定的 fs 命令,请使用权限的对象形式 { "identifier": string, "allow"?: [], "deny"?: [] }
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "fs:allow-rename", "allow": [{ "path": "$HOME/**/*" }] }, { "identifier": "fs:allow-rename", "deny": [{ "path": "$HOME/.config/**/*" }] }, { "identifier": "fs:allow-exists", "allow": [{ "path": "$APPDATA/*" }] } ]}在上面的示例中,您可以使用 exists API 访问任何 $APPDATA 子路径(不包括子目录)以及 rename。
© 2025 Tauri 贡献者。CC-BY / MIT