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

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.

主站蜘蛛池模板: 中西区| 土默特右旗| 北宁市| 镇赉县| 时尚| 元江| 兖州市| 五大连池市| 平昌县| 兴化市| 华坪县| 邵武市| 从江县| 本溪| 仙居县| 桂阳县| 伽师县| 武安市| 宁德市| 方城县| 孝感市| 沅江市| 会宁县| 望都县| 海安县| 星子县| 徐州市| 长治市| 沾化县| 凌云县| 河北省| 广平县| 贵定县| 天等县| 西峡县| 苏尼特右旗| 周口市| 浦东新区| 凤山市| 资中县| 溧阳市|