跳到内容
Tauri

path

path 模块提供了用于处理文件和目录路径的实用工具。

当 tauri.conf.json 中的 app.withGlobalTauri 设置为 true 时,也可以通过 window.__TAURI__.path 访问此包。

建议仅允许列出您使用的 API,以获得最佳的包大小和安全性。

枚举

BaseDirectory

自从

2.0.0

枚举成员

AppCache
AppCache: 16;

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

AppConfig
AppConfig: 13;

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

AppData
AppData: 14;

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

AppLocalData
AppLocalData: 15;

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

AppLog
AppLog: 17;

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

Audio
Audio: 1;

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

Cache
Cache: 2;

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

Config
Config: 3;

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

Data
Data: 4;

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

Desktop
Desktop: 18;

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

Document
Document: 6;

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

Download
Download: 7;

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

Executable
Executable: 19;

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

Font
Font: 20;

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

Home
Home: 21;

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

LocalData
LocalData: 5;

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

Picture
Picture: 8;

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

Public
Public: 9;

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

Resource
Resource: 11;

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

Runtime
Runtime: 22;

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

Temp
Temp: 12;

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

Template
Template: 23;

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

Video
Video: 10;

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

函数

appCacheDir()

function appCacheDir(): Promise<string>

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

返回值

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#L107


appConfigDir()

function appConfigDir(): Promise<string>

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

返回值

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#L56


appDataDir()

function appDataDir(): Promise<string>

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

返回值

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#L73


appLocalDataDir()

function appLocalDataDir(): Promise<string>

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

返回值

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#L90


appLogDir()

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#L519


audioDir()

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#L129


basename()

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#L649


cacheDir()

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#L151


configDir()

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#L173


dataDir()

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#L195


delimiter()

function delimiter(): string

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

  • ; 在 Windows 上
  • : 在 POSIX 上

返回值

string

自从

2.0.0

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


desktopDir()

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#L217


dirname()

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 === 'somedir');

自从

1.0.0

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


documentDir()

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#L239


downloadDir()

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#L261


executableDir()

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#L283


extname()

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#L633


fontDir()

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#L305


homeDir()

function homeDir(): Promise<string>

返回用户的 home 目录的路径。

平台特定

  • 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#L327


isAbsolute()

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#L663


join()

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#L603


localDataDir()

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#L349


normalize()

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#L588


pictureDir()

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#L371


publicDir()

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#L393


resolve()

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#L573


resolveResource()

function resolveResource(resourcePath): Promise<string>

解析资源文件的路径。

参数

参数类型描述
resourcePathstring资源的路径。必须遵循与 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#L430


resourceDir()

function resourceDir(): Promise<string>

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

返回值

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#L410


runtimeDir()

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#L453


sep()

function sep(): string

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

  • \ 在 Windows 上
  • / 在 POSIX 上

返回值

string

自从

2.0.0

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


tempDir()

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#L535


templateDir()

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#L475


videoDir()

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#L497


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