Get User Directories (Windows)

Asked

Viewed 38 times

0

Is there any way to get the system directories? In my case I am trying to get the folder Download of user logged

The most I could get was the briefcase temp of the user.

import 'dart:io';
main() {
  print(Directory.systemTemp.path);
}

1 answer

2


In Dart as far as I have seen there is no way to dynamically get directories like:

  • /home/<usuario>/* (generally linux)
  • /Users/<usuario>/*
  • c:\Users\<usuario>\*

At least not with class Directory, unfortunately then the way is to do this manually, basing me on this answer https://stackoverflow.com/a/25498458/1518921 would look something like:

import 'dart:io' show Platform, Directory;

void main() {
  String dir = "";

  Map<String, String> envVars = Platform.environment;

  if (Platform.isMacOS) {
    dir = envVars['HOME'] + "/Downloads/";
  } else if (Platform.isLinux) {
    dir = envVars['HOME'] + "/Downloads/";
  } else if (Platform.isWindows) {
    dir = envVars['UserProfile'] + "/Downloads/";
  } else {
    throw "Sistema não suportado";
  }

  final downloads = new Directory(dir);

  downloads.exists().then((existe) {
    if (existe) {
      print("Downloads existe");
    } else {
      print("Downloads NÃO existe");
    }
  });
}

But let me point out, there are other systems like Solaris, Bsds (Freebsd, Openbsd, etc), iOS, Android, depends on what you will do will have to adjust manually, and another important detail, the user can change the default folders if set up on the system, windows example, Superuser example:

So this code has minimal warranty to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.