官术网_书友最值得收藏!

Getting information from the operating system

In this recipe, you will see how to interact with the underlying operating system on which your app runs by making system calls and getting information from the system.

Getting ready

The Platform class provides you with information about the OS and the computer the app is executing on. It lives in dart:io, so we need to import this library.

How to do it...

The following script shows the use of some interesting options (refer to the code files tools\code\platform\bin\platform.dart of this chapter):

import 'dart:io';

Map env = Platform.environment;

void main() {
 print('We run from this VM: ${Platform.executable}');
// getting the OS and Dart version:
 print('Our OS is: ${Platform.operatingSystem}');
 print('We are running Dart version: ${Platform.version}');
 if (!Platform.isLinux) {
    print('We are not running on Linux here!');
  }
  // getting the number of processors:
 int noProcs = Platform.numberOfProcessors;
  print('no of processors: $noProcs');
  // getting the value of environment variables from the Map env:
  print('OS = ${env["OS"]}');
  print('HOMEDRIVE = ${env["HOMEDRIVE"]}');
  print('USERNAME = ${env["USERNAME"]}');
  print('PATH = ${env["PATH"]}');
  // getting the path to the executing Dart script:
 var path = Platform.script.path;
  print('We execute at $path');
  // on this OS we use this path separator:
 print('path separator: ${Platform.pathSeparator}');
}

When run, the above code gives the following output:

Our OS is: windows
We are running Dart version: 1.3.3 (Wed Apr 16 12:40:55 2014) on "windows_ia32"
We are not running on Linux here!
no of processors: 8
OS = Windows_NT
HOMEDRIVE = C:
USERNAME = CVO
PATH = C:\mongodb\bin;C:\MinGW\bin;...
We execute at /F:/Dartiverse/platform/bin/platform.dart
path separator: \

How it works...

Most of the options are straightforward. You can get the running VM from Platform.executable. You can get the OS from Platform.operatingSystem; this can also be tested on a Boolean property such as Platform.isLinux. The Dart version can be tested with the Platform.version property. The Platform.environment option returns a nice map structure for the environment variables of your system, so you can access their values by name, for example, for a variable envVar, use var envVar = Platform.environment["envVar"].

To get the path of the executing Dart script, you can use the path property of Platform.script because the latter returns the absolute URI of the script. When building file paths in your app, you need to know how the components in a path are separated; Platform.pathSeparator gives you this information.

There's more...

Don't confuse this class with Platform from dart:html, which returns information about the browser platform.

主站蜘蛛池模板: 林甸县| 崇仁县| 延长县| 青阳县| 凌海市| 博野县| 江华| 玉林市| 厦门市| 黄大仙区| 东兰县| 通州市| 札达县| 缙云县| 汉沽区| 田东县| 西乡县| 宁海县| 奉节县| 密云县| 莱西市| 朔州市| 余干县| 商河县| 当涂县| 南乐县| 昭觉县| 兴海县| 渭源县| 镇安县| 巴塘县| 钟祥市| 南漳县| 林口县| 河曲县| 仲巴县| 响水县| 句容市| 青龙| 巨鹿县| 玛曲县|