跳到内容
Tauri

path

path 模块提供用于处理文件和目录路径的实用程序。

tauri.conf.jsonapp.withGlobalTauri 设置为 true 时,此包也可通过 window.__TAURI__.path 访问。

建议只允许您使用的 API,以优化包大小和安全性。

2.0.0

AppCache: 16;

appCacheDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L83

AppConfig: 13;

appConfigDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L71

AppData: 14;

appDataDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L75

AppLocalData: 15;

appLocalDataDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L79

AppLog: 17;

appLogDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L87

Audio: 1;

audioDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L23

Cache: 2;

cacheDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L27

Config: 3;

configDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L31

Data: 4;

dataDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L35

Desktop: 18;

desktopDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L91

Document: 6;

documentDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L43

Download: 7;

downloadDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L47

Executable: 19;

executableDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L95

Font: 20;

fontDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L99

Home: 21;

homeDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L103

LocalData: 5;

localDataDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L39

Picture: 8;

pictureDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L51

Public: 9;

publicDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L55

Resource: 11;

resourceDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L63

Runtime: 22;

runtimeDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L107

Temp: 12;

tempDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L67

Template: 23;

templateDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L111

Video: 10;

videoDir 获取更多信息。

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L59

function appCacheDir(): Promise<string>

返回应用程序缓存文件的建议目录路径。解析为 ${cacheDir}/${bundleIdentifier},其中 bundleIdentifiertauri.conf.json 中配置的 identifier 值。

Promise<string>

import { appCacheDir } from '@tauri-apps/api/path';
const appCacheDirPath = await appCacheDir();

1.2.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L176


function appConfigDir(): Promise<string>

返回应用程序配置文件的建议目录路径。解析为 ${configDir}/${bundleIdentifier},其中 bundleIdentifiertauri.conf.json 中配置的 identifier 值。

Promise<string>

import { appConfigDir } from '@tauri-apps/api/path';
const appConfigDirPath = await appConfigDir();

1.2.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L125


function appDataDir(): Promise<string>

返回应用程序数据文件的建议目录路径。解析为 ${dataDir}/${bundleIdentifier},其中 bundleIdentifiertauri.conf.json 中配置的 identifier 值。

Promise<string>

import { appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();

1.2.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L142


function appLocalDataDir(): Promise<string>

返回应用程序本地数据文件的建议目录路径。解析为 ${localDataDir}/${bundleIdentifier},其中 bundleIdentifiertauri.conf.json 中配置的 identifier 值。

Promise<string>

import { appLocalDataDir } from '@tauri-apps/api/path';
const appLocalDataDirPath = await appLocalDataDir();

1.2.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L159


function appLogDir(): Promise<string>

返回应用程序日志文件的建议目录路径。

平台特定

  • Linux: 解析为 ${configDir}/${bundleIdentifier}/logs
  • macOS: 解析为 ${homeDir}/Library/Logs/{bundleIdentifier}
  • Windows: 解析为 ${configDir}/${bundleIdentifier}/logs

Promise<string>

import { appLogDir } from '@tauri-apps/api/path';
const appLogDirPath = await appLogDir();

1.2.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L604


function audioDir(): Promise<string>

返回用户音频目录的路径。

平台特定

  • Linux: 解析为 xdg-user-dirsXDG_MUSIC_DIR
  • macOS: 解析为 $HOME/Music
  • Windows: 解析为 {FOLDERID_Music}

Promise<string>

import { audioDir } from '@tauri-apps/api/path';
const audioDirPath = await audioDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L198


function basename(path, ext?): Promise<string>

返回 path 的最后一部分。忽略尾随目录分隔符。

参数类型描述
pathstring-
ext?string要从返回路径中删除的可选文件扩展名。

Promise<string>

import { basename } from '@tauri-apps/api/path';
const base = await basename('path/to/app.conf');
assert(base === 'app.conf');

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L734


function cacheDir(): Promise<string>

返回用户缓存目录的路径。

平台特定

  • Linux: 解析为 $XDG_CACHE_HOME$HOME/.cache
  • macOS: 解析为 $HOME/Library/Caches
  • Windows: 解析为 {FOLDERID_LocalAppData}

Promise<string>

import { cacheDir } from '@tauri-apps/api/path';
const cacheDirPath = await cacheDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L220


function configDir(): Promise<string>

返回用户配置目录的路径。

平台特定

  • Linux: 解析为 $XDG_CONFIG_HOME$HOME/.config
  • macOS: 解析为 $HOME/Library/Application Support
  • Windows: 解析为 {FOLDERID_RoamingAppData}

Promise<string>

import { configDir } from '@tauri-apps/api/path';
const configDirPath = await configDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L242


function dataDir(): Promise<string>

返回用户数据目录的路径。

平台特定

  • Linux: 解析为 $XDG_DATA_HOME$HOME/.local/share
  • macOS: 解析为 $HOME/Library/Application Support
  • Windows: 解析为 {FOLDERID_RoamingAppData}

Promise<string>

import { dataDir } from '@tauri-apps/api/path';
const dataDirPath = await dataDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L264


function delimiter(): string

返回平台特定的路径段分隔符

  • Windows 上为 ;
  • POSIX 上为 :

string

2.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L644


function desktopDir(): Promise<string>

返回用户桌面目录的路径。

平台特定

  • Linux: 解析为 xdg-user-dirsXDG_DESKTOP_DIR
  • macOS: 解析为 $HOME/Desktop
  • Windows: 解析为 {FOLDERID_Desktop}

Promise<string>

import { desktopDir } from '@tauri-apps/api/path';
const desktopPath = await desktopDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L286


function dirname(path): Promise<string>

返回给定 path 的父目录。忽略尾随目录分隔符。

参数类型
pathstring

Promise<string>

import { dirname } from '@tauri-apps/api/path';
const dir = await dirname('/path/to/somedir/');
assert(dir === '/path/to');

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L703


function documentDir(): Promise<string>

返回用户文档目录的路径。

Promise<string>

import { documentDir } from '@tauri-apps/api/path';
const documentDirPath = await documentDir();

平台特定

  • Linux: 解析为 xdg-user-dirsXDG_DOCUMENTS_DIR
  • macOS: 解析为 $HOME/Documents
  • Windows: 解析为 {FOLDERID_Documents}

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L308


function downloadDir(): Promise<string>

返回用户下载目录的路径。

平台特定

  • Linux: 解析为 xdg-user-dirsXDG_DOWNLOAD_DIR
  • macOS: 解析为 $HOME/Downloads
  • Windows: 解析为 {FOLDERID_Downloads}

Promise<string>

import { downloadDir } from '@tauri-apps/api/path';
const downloadDirPath = await downloadDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L330


function executableDir(): Promise<string>

返回用户可执行目录的路径。

平台特定

  • Linux: 解析为 $XDG_BIN_HOME/../bin$XDG_DATA_HOME/../bin$HOME/.local/bin
  • macOS: 不支持。
  • Windows: 不支持。

Promise<string>

import { executableDir } from '@tauri-apps/api/path';
const executableDirPath = await executableDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L352


function extname(path): Promise<string>

返回 path 的扩展名。

参数类型
pathstring

Promise<string>

import { extname } from '@tauri-apps/api/path';
const ext = await extname('/path/to/file.html');
assert(ext === 'html');

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L718


function fontDir(): Promise<string>

返回用户字体目录的路径。

平台特定

  • Linux: 解析为 $XDG_DATA_HOME/fonts$HOME/.local/share/fonts
  • macOS: 解析为 $HOME/Library/Fonts
  • Windows: 不支持。

Promise<string>

import { fontDir } from '@tauri-apps/api/path';
const fontDirPath = await fontDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L374


function homeDir(): Promise<string>

返回用户主目录的路径。

平台特定

  • Linux: 解析为 $HOME
  • macOS: 解析为 $HOME
  • Windows: 解析为 {FOLDERID_Profile}

Promise<string>

import { homeDir } from '@tauri-apps/api/path';
const homeDirPath = await homeDir();

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L396


function isAbsolute(path): Promise<boolean>

返回路径是否为绝对路径。

参数类型
pathstring

Promise<boolean>

import { isAbsolute } from '@tauri-apps/api/path';
assert(await isAbsolute('/home/tauri'));

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L748


function join(...paths): Promise<string>

使用平台特定分隔符将所有给定的 path 段连接在一起,然后规范化生成的路径。

参数类型
...pathsstring[]

Promise<string>

import { join, appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
const path = await join(appDataDirPath, 'users', 'tauri', 'avatar.png');

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L688


function localDataDir(): Promise<string>

返回用户本地数据目录的路径。

平台特定

  • Linux: 解析为 $XDG_DATA_HOME$HOME/.local/share
  • macOS: 解析为 $HOME/Library/Application Support
  • Windows: 解析为 {FOLDERID_LocalAppData}

Promise<string>

import { localDataDir } from '@tauri-apps/api/path';
const localDataDirPath = await localDataDir();

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L418


function normalize(path): Promise<string>

规范化给定的 path,解析 '..''.' 段并解析符号链接。

参数类型
pathstring

Promise<string>

import { normalize, appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
const path = await normalize(`${appDataDirPath}/../users/tauri/avatar.png`);

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L673


function pictureDir(): Promise<string>

返回用户图片目录的路径。

平台特定

  • Linux:解析为 xdg-user-dirsXDG_PICTURES_DIR
  • macOS:解析为 $HOME/Pictures
  • Windows:解析为 {FOLDERID_Pictures}

Promise<string>

import { pictureDir } from '@tauri-apps/api/path';
const pictureDirPath = await pictureDir();

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L440


function publicDir(): Promise<string>

返回用户公共目录的路径。

平台特定

  • Linux:解析为 xdg-user-dirsXDG_PUBLICSHARE_DIR
  • macOS:解析为 $HOME/Public
  • Windows:解析为 {FOLDERID_Public}

Promise<string>

import { publicDir } from '@tauri-apps/api/path';
const publicDirPath = await publicDir();

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L462


function resolve(...paths): Promise<string>

将一系列 pathspath 段解析为绝对路径。

参数类型
...pathsstring[]

Promise<string>

import { resolve, appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
const path = await resolve(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L658


function resolveResource(resourcePath): Promise<string>

解析资源文件的路径。

参数类型描述
资源路径string资源的路径。必须遵循 tauri.conf.json > bundle > resources 中定义的相同语法,即保留子文件夹和父目录组件 (../)。

Promise<string>

资源的完整路径。

import { resolveResource } from '@tauri-apps/api/path';
const resourcePath = await resolveResource('script.sh');

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L515


function resourceDir(): Promise<string>

返回应用程序资源目录的路径。要解析资源路径,请参阅 resolveResource

尽管我们提供了此函数解析到的确切路径,但这并非约定,将来可能会有所更改

  • Windows:解析到包含主可执行文件的目录。
  • Linux:在 AppImage 中运行时,APPDIR 变量将被设置为应用程序的挂载位置,资源目录将是 ${APPDIR}/usr/lib/${exe_name}。如果不在 AppImage 中运行,路径是 /usr/lib/${exe_name}。从 src-tauri/target/(debug|release)/ 运行应用程序时,路径是 ${exe_dir}/../lib/${exe_name}
  • macOS:解析到 ${exe_dir}/../Resources(在 .app 内部)。
  • iOS:解析到 ${exe_dir}/assets
  • Android:目前资源作为资产存储在 APK 中,因此它不是一个普通的文件系统路径,我们在此处返回一个特殊的 URI 前缀 asset:///,可与 文件系统插件 一起使用,

Promise<string>

import { resourceDir } from '@tauri-apps/api/path';
const resourceDirPath = await resourceDir();

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L495


function runtimeDir(): Promise<string>

返回用户运行时目录的路径。

平台特定

  • Linux:解析为 $XDG_RUNTIME_DIR
  • macOS: 不支持。
  • Windows: 不支持。

Promise<string>

import { runtimeDir } from '@tauri-apps/api/path';
const runtimeDirPath = await runtimeDir();

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L538


function sep(): string

返回平台特定的路径段分隔符

  • Windows 上为 \
  • POSIX 上为 /

string

2.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L633


function tempDir(): Promise<string>

返回一个临时目录。

Promise<string>

import { tempDir } from '@tauri-apps/api/path';
const temp = await tempDir();

2.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L620


function templateDir(): Promise<string>

返回用户模板目录的路径。

平台特定

  • Linux:解析为 xdg-user-dirsXDG_TEMPLATES_DIR
  • macOS: 不支持。
  • Windows:解析为 {FOLDERID_Templates}

Promise<string>

import { templateDir } from '@tauri-apps/api/path';
const templateDirPath = await templateDir();

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L560


function videoDir(): Promise<string>

返回用户视频目录的路径。

平台特定

  • Linux:解析为 xdg-user-dirsXDG_VIDEOS_DIR
  • macOS:解析为 $HOME/Movies
  • Windows:解析为 {FOLDERID_Videos}

Promise<string>

import { videoDir } from '@tauri-apps/api/path';
const videoDirPath = await videoDir();

1.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L582


© 2025 Tauri 贡献者。CC-BY / MIT