1# @ohos.file.environment (目录环境能力) 2 3该模块提供环境目录能力,获取内存存储根目录、公共文件根目录的JS接口。 4 5> **说明:** 6> 7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import environment from '@ohos.file.environment'; 13``` 14 15## environment.getUserDownloadDir<sup>11+</sup> 16 17getUserDownloadDir(): string 18 19获取当前用户预授权下载目录的沙箱路径,该接口仅对特定设备开放。 20 21**需要权限**:ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY 22 23**系统能力**:SystemCapability.FileManagement.File.Environment.FolderObtain 24 25**返回值:** 26 27| 类型 | 说明 | 28| --------------------- |---------------------| 29| string | 返回当前用户预授权下载目录的沙箱路径。 | 30 31**错误码:** 32 33以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 34 35| 错误码ID | 错误信息 | 36|----------| --------- | 37| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 38| 801 | Capability not supported. | 39| 13900042 | Unknown error | 40 41**示例:** 42 43```ts 44import { BusinessError } from '@ohos.base'; 45function getUserDownloadDirExample() { 46 try { 47 let path = environment.getUserDownloadDir(); 48 console.log(`success to getUserDownloadDir: ${JSON.stringify(path)}`); 49 } catch (error) { 50 console.error(`failed to getUserDownloadDir because: ${JSON.stringify(error)}`); 51 } 52} 53``` 54 55## environment.getUserDesktopDir<sup>11+</sup> 56 57getUserDesktopDir(): string 58 59获取当前用户预授权桌面目录的沙箱路径,该接口仅对特定设备开放。 60 61**需要权限**:ohos.permission.READ_WRITE_DESKTOP_DIRECTORY 62 63**系统能力**:SystemCapability.FileManagement.File.Environment.FolderObtain 64 65**返回值:** 66 67| 类型 | 说明 | 68| --------------------- |---------------------| 69| string | 返回当前用户预授权桌面目录的沙箱路径。 | 70 71**错误码:** 72 73以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 74 75| 错误码ID | 错误信息 | 76|----------| --------- | 77| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 78| 801 | Capability not supported. | 79| 13900042 | Unknown error | 80 81**示例:** 82 83```ts 84import { BusinessError } from '@ohos.base'; 85function getUserDesktopDirExample() { 86 try { 87 let path = environment.getUserDesktopDir(); 88 console.log(`success to getUserDesktopDir: ${JSON.stringify(path)}`); 89 } catch (error) { 90 console.error(`failed to getUserDesktopDir because: ${JSON.stringify(error)}`); 91 } 92} 93``` 94 95## environment.getUserDocumentDir<sup>11+</sup> 96 97getUserDocumentDir(): string 98 99获取当前用户预授权文档目录的沙箱路径,该接口仅对特定设备开放。 100 101**需要权限**:ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY 102 103**系统能力**:SystemCapability.FileManagement.File.Environment.FolderObtain 104 105**返回值:** 106 107| 类型 | 说明 | 108| --------------------- |---------------------| 109| string | 返回当前用户预授权文档目录的沙箱路径。 | 110 111**错误码:** 112 113以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 114 115| 错误码ID | 错误信息 | 116|----------| --------- | 117| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 118| 801 | Capability not supported. | 119| 13900042 | Unknown error | 120 121**示例:** 122 123```ts 124import { BusinessError } from '@ohos.base'; 125function getUserDocumentDirExample() { 126 try { 127 let path = environment.getUserDocumentDir(); 128 console.log(`success to getUserDocumentDir: ${JSON.stringify(path)}`); 129 } catch (error) { 130 console.error(`failed to getUserDocumentDir because: ${JSON.stringify(error)}`); 131 } 132} 133``` 134