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

  • Dart Cookbook
  • Ivo Balbaert
  • 403字
  • 2021-08-05 17:42:46

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.

主站蜘蛛池模板: 乌鲁木齐县| 庆安县| 孝义市| 山阴县| 娄烦县| 涞源县| 阜康市| 威远县| 东山县| 梁山县| 大名县| 宜昌市| 张家口市| 桃园市| 惠安县| 宁都县| 宜君县| 新河县| 仁寿县| 合阳县| 石柱| 桐柏县| 呈贡县| 黄陵县| 陇川县| 廊坊市| 常熟市| 汝城县| 广安市| 贵定县| 米林县| 武乡县| 洪雅县| 宁明县| 海晏县| 广平县| 宝鸡市| 张家界市| 基隆市| 嘉义县| 东至县|