js-apis-environment.md 3.1 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2
# Environment

E
ester.zhou 已提交
3 4
This module provides JS APIs for obtaining the root directories of the storage and public files.

E
ester.zhou 已提交
5
> **NOTE**
E
ester.zhou 已提交
6 7 8
>
> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs of this module are system APIs and cannot be called by third-party applications.
E
add doc  
ester.zhou 已提交
9 10 11 12 13 14 15 16 17 18 19

## Modules to Import

```js
import environment from '@ohos.environment';
```

## environment.getStorageDataDir

getStorageDataDir():Promise<string>

E
ester.zhou 已提交
20 21 22
Obtains the root directory of the storage. This API uses a promise to return the result.

**System capability**: SystemCapability.FileManagement.File.Environment
E
add doc  
ester.zhou 已提交
23 24 25

- Return value

E
ester.zhou 已提交
26
  | Type                 | Description            |
E
add doc  
ester.zhou 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  | --------------------- | ---------------- |
  | Promise<string> | Promise returned with the root directory of the storage.|

- Example

  ```js
  environment.getStorageDataDir().then(function(path){
      console.info("getStorageDataDir successfully:"+ path);
  }).catch(function(error){
      console.info("getStorageDataDir failed with error:"+ error);
  });
  ```

## environment.getStorageDataDir

getStorageDataDir(callback:AsyncCallback<string>):void

E
ester.zhou 已提交
44 45 46
Obtains the root directory of the storage. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.FileManagement.File.Environment
E
add doc  
ester.zhou 已提交
47 48 49

- Parameters

E
ester.zhou 已提交
50
  | Name  | Type                       | Mandatory| Description                            |
E
add doc  
ester.zhou 已提交
51
  | -------- | --------------------------- | ---- | -------------------------------- |
E
ester.zhou 已提交
52
  | callback | AsyncCallback<string> | Yes  | Asynchronous callback used to return the root directory of the storage.|
E
add doc  
ester.zhou 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65

- Example

  ```js
  environment.getStorageDataDir(function(error, path){
      // do something
  });
  ```

## environment.getUserDataDir

getUserDataDir():Promise<string>

E
ester.zhou 已提交
66 67 68
Obtains the root directory of public files. This API uses a promise to return the result.

**System capability**: SystemCapability.FileManagement.File.Environment
E
add doc  
ester.zhou 已提交
69 70 71

- Return value

E
ester.zhou 已提交
72
  | Type                 | Description              |
E
add doc  
ester.zhou 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  | --------------------- | ------------------ |
  | Promise<string> | Promise returned with the root directory of public files.|

- Example

  ```js
  environment.getUserDataDir().then(function(path){
      console.info("getUserDataDir successfully:"+ path);
  }).catch(function(error){
      console.info("getUserDataDir failed with error:"+ error);
  });
  ```

## environment.getUserDataDir

getUserDataDir(callback:AsyncCallback<string>): void

E
ester.zhou 已提交
90 91 92
Obtains the root directory of public files. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.FileManagement.File.Environment
E
add doc  
ester.zhou 已提交
93 94 95

- Parameters

E
ester.zhou 已提交
96
  | Name  | Type                       | Mandatory| Description                            |
E
add doc  
ester.zhou 已提交
97
  | -------- | --------------------------- | ---- | -------------------------------- |
E
ester.zhou 已提交
98
  | callback | AsyncCallback<string> | Yes  | Asynchronous callback used to return the root directory of public files.|
E
add doc  
ester.zhou 已提交
99 100 101 102 103 104 105 106
  
- Example

  ```js
  environment.getUserDataDir(function(error, path){
      // do something
  });
  ```