路徑
路徑模組提供用於處理檔案和目錄路徑的工具程式。
當 tauri.conf.json
中的 build.withGlobalTauri
設為 true
時,也可以透過 window.__TAURI__.path
存取此套件。
必須將 API 新增至 tauri.conf.json
中的 tauri.allowlist.path
{
"tauri": {
"allowlist": {
"path": {
"all": true, // enable all Path APIs
}
}
}
}
建議僅允許您使用的 API,以最佳化套件大小和安全性。
參考
BaseDirectory
重新匯出 BaseDirectory
變數
delimiter
Const
delimiter:";"
|":"
提供特定於平台的路徑區段分隔符號
- Windows 上為
;
- POSIX 上為
:
自: 1.0.0
定義於: path.ts:660
sep
Const
sep:"\\"
|"/"
提供特定於平台的路徑區段分隔符號
- Windows 上為
\
- POSIX 上為
/
自: 1.0.0
定義於: path.ts:651
函式
appCacheDir
appCacheDir():
Promise
<string
>
傳回建議的應用程式快取檔案目錄路徑。解析為 ${cacheDir}/${bundleIdentifier}
,其中 bundleIdentifier
是 tauri.conf.json
中設定的 tauri.bundle.identifier
值。
範例
import { appCacheDir } from '@tauri-apps/api/path';
const appCacheDirPath = await appCacheDir();
自: 1.2.0
傳回:Promise
<字串
>
appConfigDir
appConfigDir():
Promise
<字串
>
傳回建議的應用程式設定檔目錄路徑。解析為 ${configDir}/${bundleIdentifier}
,其中 bundleIdentifier
是在 tauri.conf.json
中設定的 tauri.bundle.identifier
值。
範例
import { appConfigDir } from '@tauri-apps/api/path';
const appConfigDirPath = await appConfigDir();
自: 1.2.0
傳回:Promise
<字串
>
appDataDir
appDataDir():
Promise
<字串
>
傳回建議的應用程式資料檔目錄路徑。解析為 ${dataDir}/${bundleIdentifier}
,其中 bundleIdentifier
是在 tauri.conf.json
中設定的 tauri.bundle.identifier
值。
範例
import { appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
自: 1.2.0
傳回:Promise
<字串
>
appDir
appDir():
Promise
<字串
>
傳回建議的應用程式設定檔目錄路徑。
已棄用
自 1.2.0 起:將在 2.0.0 中移除。請改用 appConfigDir 或 appDataDir。
自: 1.0.0
傳回:Promise
<字串
>
appLocalDataDir
appLocalDataDir():
Promise
<string
>
傳回建議的應用程式本機資料檔案目錄路徑。解析為 ${localDataDir}/${bundleIdentifier}
,其中 bundleIdentifier
是在 tauri.conf.json
中設定的 tauri.bundle.identifier
值。
範例
import { appLocalDataDir } from '@tauri-apps/api/path';
const appLocalDataDirPath = await appLocalDataDir();
自: 1.2.0
傳回:Promise
<字串
>
appLogDir
appLogDir():
Promise
<string
>
傳回建議的應用程式日誌檔案目錄路徑。
特定於平台
- Linux:解析為
${configDir}/${bundleIdentifier}/logs
。 - macOS:解析為
${homeDir}/Library/Logs/{bundleIdentifier}
- Windows:解析為
${configDir}/${bundleIdentifier}/logs
。
範例
import { appLogDir } from '@tauri-apps/api/path';
const appLogDirPath = await appLogDir();
自: 1.2.0
傳回:Promise
<字串
>
audioDir
audioDir():
Promise
<string
>
傳回使用者的音訊目錄路徑。
特定於平台
- Linux:解析為
xdg-user-dirs
的XDG_MUSIC_DIR
。 - macOS:解析為
$HOME/Music
。 - Windows:解析為
{FOLDERID_Music}
。
範例
import { audioDir } from '@tauri-apps/api/path';
const audioDirPath = await audioDir();
自: 1.0.0
傳回:Promise
<字串
>
basename
basename(
path
:string
,ext?
:string
):Promise
<string
>
傳回 path
的最後一部分。會忽略尾隨目錄分隔符號。
範例
import { basename, resolveResource } from '@tauri-apps/api/path';
const resourcePath = await resolveResource('app.conf');
const base = await basename(resourcePath);
assert(base === 'app.conf');
自: 1.0.0
參數
名稱 | 類型 | 說明 |
---|---|---|
路徑 | string | - |
ext? | string | 要從傳回路徑中移除的選用檔案副檔名。 |
傳回:Promise
<字串
>
cacheDir
cacheDir():
Promise
<string
>
傳回使用者快取目錄的路徑。
特定於平台
- Linux:解析為
$XDG_CACHE_HOME
或$HOME/.cache
。 - macOS:解析為
$HOME/Library/Caches
。 - Windows:解析為
{FOLDERID_LocalAppData}
。
範例
import { cacheDir } from '@tauri-apps/api/path';
const cacheDirPath = await cacheDir();
自: 1.0.0
傳回:Promise
<字串
>
configDir
configDir():
Promise
<string
>
傳回使用者設定檔目錄的路徑。
特定於平台
- Linux:解析為
$XDG_CONFIG_HOME
或$HOME/.config
。 - macOS:解析為
$HOME/Library/Application Support
。 - Windows:解析為
{FOLDERID_RoamingAppData}
。
範例
import { configDir } from '@tauri-apps/api/path';
const configDirPath = await configDir();
自: 1.0.0
傳回:Promise
<字串
>
dataDir
dataDir():
Promise
<string
>
傳回使用者資料目錄的路徑。
特定於平台
- Linux:解析為
$XDG_DATA_HOME
或$HOME/.local/share
。 - macOS:解析為
$HOME/Library/Application Support
。 - Windows:解析為
{FOLDERID_RoamingAppData}
。
範例
import { dataDir } from '@tauri-apps/api/path';
const dataDirPath = await dataDir();
自: 1.0.0
傳回:Promise
<字串
>
desktopDir
desktopDir():
Promise
<string
>
傳回使用者桌面目錄的路徑。
特定於平台
- Linux:解析為
xdg-user-dirs
的XDG_DESKTOP_DIR
。 - macOS:解析為
$HOME/Desktop
。 - Windows:解析為
{FOLDERID_Desktop}
。
範例
import { desktopDir } from '@tauri-apps/api/path';
const desktopPath = await desktopDir();
自: 1.0.0
傳回:Promise
<字串
>
dirname
dirname(
path
:string
):Promise
<string
>
傳回 path
的目錄名稱。尾隨目錄分隔符號會被忽略。
範例
import { dirname, appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
const dir = await dirname(appDataDirPath);
自: 1.0.0
參數
名稱 | 類型 |
---|---|
路徑 | string |
傳回:Promise
<字串
>
documentDir
documentDir():
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
傳回:Promise
<字串
>
downloadDir
downloadDir():
Promise
<string
>
傳回使用者的下載目錄路徑。
特定於平台
- Linux:解析為
xdg-user-dirs
的XDG_DOWNLOAD_DIR
。 - macOS:解析為
$HOME/Downloads
。 - Windows:解析為
{FOLDERID_Downloads}
。
範例
import { downloadDir } from '@tauri-apps/api/path';
const downloadDirPath = await downloadDir();
自: 1.0.0
傳回:Promise
<字串
>
executableDir
executableDir():
Promise
<string
>
傳回使用者的可執行檔目錄路徑。
特定於平台
- Linux:解析為
$XDG_BIN_HOME/../bin
或$XDG_DATA_HOME/../bin
或$HOME/.local/bin
。 - macOS:不支援。
- Windows:不支援。
範例
import { executableDir } from '@tauri-apps/api/path';
const executableDirPath = await executableDir();
自: 1.0.0
傳回:Promise
<字串
>
extname
extname(
path
:string
):Promise
<string
>
傳回 path
的副檔名。
範例
import { extname, resolveResource } from '@tauri-apps/api/path';
const resourcePath = await resolveResource('app.conf');
const ext = await extname(resourcePath);
assert(ext === 'conf');
自: 1.0.0
參數
名稱 | 類型 |
---|---|
路徑 | string |
傳回:Promise
<字串
>
fontDir
fontDir():
Promise
<string
>
傳回使用者的字型目錄路徑。
特定於平台
- Linux:解析為
$XDG_DATA_HOME/fonts
或$HOME/.local/share/fonts
。 - macOS:解析為
$HOME/Library/Fonts
。 - Windows:不支援。
範例
import { fontDir } from '@tauri-apps/api/path';
const fontDirPath = await fontDir();
自: 1.0.0
傳回:Promise
<字串
>
homeDir
homeDir():
Promise
<string
>
傳回使用者的家目錄路徑。
特定於平台
- Linux:解析為
$HOME
。 - macOS:解析為
$HOME
。 - Windows:解析為
{FOLDERID_Profile}
。
範例
import { homeDir } from '@tauri-apps/api/path';
const homeDirPath = await homeDir();
自: 1.0.0
傳回:Promise
<字串
>
isAbsolute
isAbsolute(
path
:string
):Promise
<boolean
>
傳回路徑是否為絕對路徑。
範例
import { isAbsolute } from '@tauri-apps/api/path';
assert(await isAbsolute('/home/tauri'));
自: 1.0.0
參數
名稱 | 類型 |
---|---|
路徑 | string |
傳回:Promise
<boolean
>
join
join(...
paths
:string
[]):Promise
<string
>
使用特定於平台的分隔符號作為分隔符號,將所有給定的 path
片段串連在一起,然後標準化產生的路徑。
範例
import { join, appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
const path = await join(appDataDirPath, 'users', 'tauri', 'avatar.png');
自: 1.0.0
參數
名稱 | 類型 |
---|---|
...路徑 | string [] |
傳回:Promise
<字串
>
localDataDir
localDataDir():
Promise
<string
>
傳回使用者本機資料目錄的路徑。
特定於平台
- Linux:解析為
$XDG_DATA_HOME
或$HOME/.local/share
。 - macOS:解析為
$HOME/Library/Application Support
。 - Windows:解析為
{FOLDERID_LocalAppData}
。
範例
import { localDataDir } from '@tauri-apps/api/path';
const localDataDirPath = await localDataDir();
自: 1.0.0
傳回:Promise
<字串
>
logDir
logDir():
Promise
<string
>
傳回建議的記錄目錄的路徑。
已棄用
自 1.2.0 起:將在 2.0.0 中移除。請改用 appLogDir。
自: 1.0.0
傳回:Promise
<字串
>
normalize
normalize(
path
:string
):Promise
<string
>
標準化給定的 path
,解析 '..'
和 '.'
片段,並解析符號連結。
範例
import { normalize, appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
const path = await normalize(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
自: 1.0.0
參數
名稱 | 類型 |
---|---|
路徑 | string |
傳回:Promise
<字串
>
pictureDir
pictureDir():
Promise
<string
>
傳回使用者圖片目錄的路徑。
特定於平台
- Linux:解析為
xdg-user-dirs
的XDG_PICTURES_DIR
。 - macOS:解析為
$HOME/Pictures
。 - Windows:解析為
{FOLDERID_Pictures}
。
範例
import { pictureDir } from '@tauri-apps/api/path';
const pictureDirPath = await pictureDir();
自: 1.0.0
傳回:Promise
<字串
>
publicDir
publicDir():
Promise
<string
>
傳回使用者的公用目錄路徑。
特定平台
- Linux: 解析為
xdg-user-dirs
的XDG_PUBLICSHARE_DIR
。 - macOS: 解析為
$HOME/Public
。 - Windows: 解析為
{FOLDERID_Public}
。
範例
import { publicDir } from '@tauri-apps/api/path';
const publicDirPath = await publicDir();
自: 1.0.0
傳回:Promise
<字串
>
resolve
resolve(...
paths
:string
[]):Promise
<string
>
將一系列 paths
或 path
片段解析為絕對路徑。
範例
import { resolve, appDataDir } from '@tauri-apps/api/path';
const appDataDirPath = await appDataDir();
const path = await resolve(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
自: 1.0.0
參數
名稱 | 類型 |
---|---|
...路徑 | string [] |
傳回:Promise
<字串
>
resolveResource
resolveResource(
resourcePath
:string
):Promise
<string
>
解析資源檔案的路徑。
範例
import { resolveResource } from '@tauri-apps/api/path';
const resourcePath = await resolveResource('script.sh');
自: 1.0.0
參數
名稱 | 類型 | 說明 |
---|---|---|
resourcePath | string | 資源的路徑。 必須遵循 tauri.conf.json > tauri > bundle > resources 中定義的語法,亦即保留子資料夾和父目錄元件 (../ )。 |
傳回:Promise
<字串
>
資源的完整路徑。
resourceDir
resourceDir():
Promise
<string
>
傳回應用程式資源目錄路徑。若要解析資源路徑,請參閱 [[resolveResource | resolveResource API
]]。
範例
import { resourceDir } from '@tauri-apps/api/path';
const resourceDirPath = await resourceDir();
自: 1.0.0
傳回:Promise
<字串
>
runtimeDir
runtimeDir():
Promise
<string
>
傳回使用者執行時期目錄的路徑。
特定於平台
- Linux: 解析為
$XDG_RUNTIME_DIR
。 - macOS:不支援。
- Windows:不支援。
範例
import { runtimeDir } from '@tauri-apps/api/path';
const runtimeDirPath = await runtimeDir();
自: 1.0.0
傳回:Promise
<字串
>
templateDir
templateDir():
Promise
<string
>
傳回使用者範本目錄的路徑。
特定於平台
- Linux: 解析為
xdg-user-dirs
'XDG_TEMPLATES_DIR
。 - macOS:不支援。
- Windows: 解析為
{FOLDERID_Templates}
。
範例
import { templateDir } from '@tauri-apps/api/path';
const templateDirPath = await templateDir();
自: 1.0.0
傳回:Promise
<字串
>
videoDir
videoDir():
Promise
<string
>
傳回使用者影片目錄的路徑。
特定於平台
- Linux: 解析為
xdg-user-dirs
'XDG_VIDEOS_DIR
。 - macOS: 解析為
$HOME/Movies
。 - Windows:解析為
{FOLDERID_Videos}
。
範例
import { videoDir } from '@tauri-apps/api/path';
const videoDirPath = await videoDir();
自: 1.0.0
傳回:Promise
<字串
>