路径
路径模块提供用于处理文件和目录路径的工具函数。
当在 tauri.conf.json
中将 app.withGlobalTauri
设置为 true
时,此包也可以通过 window.__TAURI__.path
访问。
建议仅允许列表中包含您使用的 API,以优化包大小和安全。
枚举
基本目录
自
2.0.0
枚举成员
应用缓存
AppCache: 16;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L35
应用配置
AppConfig: 13;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L32
应用数据
AppData: 14;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L33
本地数据
AppLocalData: 15;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L34
应用日志
AppLog: 17;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L36
音频
Audio: 1;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L20
缓存
Cache: 2;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L21
配置
Config: 3;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L22
数据
Data: 4;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L23
桌面
Desktop: 18;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L37
文档
Document: 6;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L25
下载
Download: 7;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L26
可执行文件
Executable: 19;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L38
字体
Font: 20;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L39
主目录
Home: 21;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L40
本地数据
LocalData: 5;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L24
图片
Picture: 8;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L27
公共
Public: 9;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L28
资源
Resource: 11;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L30
运行时
Runtime: 22;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L41
临时
Temp: 12;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L31
模板
Template: 23;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L42
视频
Video: 10;
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/path.ts#L29
函数
appCacheDir()
function appCacheDir(): Promise<string>
返回应用缓存文件的推荐目录路径。解析为 ${cacheDir}/${bundleIdentifier}
,其中 bundleIdentifier
是在 tauri.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#L107
appConfigDir()
function appConfigDir(): Promise<string>
返回应用配置文件的推荐目录路径。解析为 ${configDir}/${bundleIdentifier}
,其中 bundleIdentifier
是在 tauri.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#L56
appDataDir()
function appDataDir(): Promise<string>
返回应用数据文件的推荐目录路径。解析为 ${dataDir}/${bundleIdentifier}
,其中 bundleIdentifier
是在 tauri.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#L73
appLocalDataDir()
function appLocalDataDir(): Promise<string>
返回应用本地数据文件的推荐目录路径。解析为 ${localDataDir}/${bundleIdentifier}
,其中 bundleIdentifier
是在 tauri.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#L90
appLogDir()
function appLogDir(): Promise<string>
返回应用日志文件的推荐目录路径。
特定平台
- Linux: 解析为
${configDir}/${bundleIdentifier}/logs
。 - macOS: 解析为
$HOME/Music
- 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-dirs
的XDG_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>
返回路径的最后部分,忽略目录分隔符。
参数
参数 | 类型 | 描述 |
---|---|---|
路径 | string | - |
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-dirs
的XDG_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>
返回路径的目录名。忽略尾随目录分隔符。
参数
参数 | 类型 |
---|---|
路径 | string |
返回
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-dirs
的XDG_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-dirs
的XDG_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>
返回路径的扩展名。
参数
参数 | 类型 |
---|---|
路径 | string |
返回
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>
返回用户主目录的路径。
特定平台
- 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>
返回路径是否为绝对路径。
参数
参数 | 类型 |
---|---|
路径 | string |
返回
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>
使用特定平台分隔符作为分隔符,将所有给定的 路径
段连接起来,然后规范化结果路径。
参数
参数 | 类型 |
---|---|
…路径 | string [] |
返回
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>
规范化给定的 路径
,解析 '..'+'
和 '.'
段,并解析符号链接。
参数
参数 | 类型 |
---|---|
路径 | string |
返回
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-dirs
的XDG_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-dirs
的XDG_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>
解析一系列 路径
或 路径段
到绝对路径。
参数
参数 | 类型 |
---|---|
…路径 | string [] |
返回
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>
解析资源文件的路径。
参数
参数 | 类型 | 描述 |
---|---|---|
resourcePath | 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#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-dirs
的XDG_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-dirs
的XDG_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