如何在Dart中告诉用户的主目录在哪里?

Set*_*add 5 dart

我想在文件中存储一小部分信息,在命令行应用程序的运行之间保留.可能最好的位置是用户主目录中的一个小文件.

我想将一个property/config文件写入用户的主目录.对于Windows,Mac和Linux,我怎么知道用户的主目录是什么?我正在使用Dart.

use*_*109 6

识别操作系统然后使用指定的环境变量用于该特定操作系统.您可以从平台读取OS /环境变量.例如:

  1. OS : String os = Platform.operatingSystem;各种检查等isLinux,isAndroid给出
  2. Map<String, String> envVars = Platform.environment;

一个例子:

import 'dart:io' show Platform, stdout;

void main() {
  String os = Platform.operatingSystem;
  String home = "";
  Map<String, String> envVars = Platform.environment;
  if (Platform.isMacOS) {
    home = envVars['HOME'];
  } else if (Platform.isLinux) {
    home = envVars['HOME'];
  } else if (Platform.isWindows) {
    home = envVars['UserProfile'];
  }
  stdout.writeln(home);
}
Run Code Online (Sandbox Code Playgroud)

家庭目录取自这里:http://en.wikipedia.org/wiki/Home_directory