xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
environment.h
Go to the documentation of this file.
1 #pragma once
5 #include <map>
6 #include <cstdlib>
7 #include <thread>
8 #include <vector>
9 
10 #include "argument_exception.h"
11 #include "compiler.h"
12 #include "core_export.h"
13 #include "cpp_language.h"
15 #include "guid.h"
16 #include "operating_system.h"
17 #include "platform_id.h"
18 #include "processor.h"
19 #include "static.h"
20 #include "ustring.h"
21 #include "version.h"
24 #include "io/directory.h"
25 
27 namespace xtd {
34  class core_export_ environment final static_ {
35  public:
93  enum class special_folder {
95  desktop = 0,
97  programs = 2,
99  my_documents = 5,
101  personal = 5,
103  favorites = 6,
105  startup = 7,
107  recent = 8,
109  send_to = 9,
111  start_menu = 11,
113  my_music = 13,
115  my_videos = 14,
117  desktop_directory = 16,
119  my_computer = 17,
121  network_shortcuts = 19,
123  fonts = 20,
125  templates = 21,
127  common_start_menu = 22,
129  common_programs = 23,
131  common_startup = 24,
133  common_desktop_directory = 25,
135  application_data = 26,
137  printer_shortcuts = 27,
139  local_application_data = 28,
141  internet_cache = 32,
143  cookies = 33,
145  history = 34,
147  common_application_data = 35,
149  windows = 36,
151  system = 37,
153  program_files = 38,
155  my_pictures = 39,
157  user_profile = 40,
159  system_x86 = 41,
161  program_files_x86 = 42,
163  common_program_files = 43,
165  common_program_files_x86 = 44,
167  common_templates = 45,
169  common_documents = 46,
171  common_admin_tools = 47,
173  admin_tools = 48,
175  common_music = 53,
177  common_pictures = 54,
179  common_videos = 55,
181  resources = 56,
183  localized_resources = 57,
185  common_oem_links = 58,
187  cd_burning = 59,
189  home = 64
190  };
191 
196  none = 0,
198  do_not_verify = 16384,
200  create = 32768
201  };
202 
204 
211  static xtd::ustring command_line() {return xtd::ustring::join(" ", get_command_line_args());}
212 
216  static xtd::compiler compiler;
217  return compiler;
218  }
219 
224  return cpp_language;
225  }
226 
231  static xtd::ustring current_directory();
232 
239  static void current_directory(const xtd::ustring& directory_name);
240 
243  static std::thread::id current_thread_id() {return std::this_thread::get_id();}
244 
250  static int exit_code();
256  static void exit_code(int value);
257 
260  static bool is_64_bit_operating_system() {return os_version().is_64_bit();}
261 
264  static bool is_64_bit_process() {return sizeof(size_t) == 8;}
265 
269  static xtd::ustring machine_name();
270 
273  static xtd::ustring new_line();
274 
278  template <class char_t, class traits_t>
279  static std::basic_ostream<char_t, traits_t>& new_line(std::basic_ostream<char_t, traits_t>& os) {
280  os.put(os.widen('\n'));
281  os.flush();
282  return os;
283  }
284 
287  static xtd::operating_system os_version();
288 
291  static uint32_t processor_count() {return processor_information().core_count();}
292 
295  static xtd::processor processor_information();
296 
301  }
302 
307 
310  static size_t system_page_size();
311 
314  static std::chrono::milliseconds tick_count();
315 
318  static xtd::ustring user_domain_name();
319 
325  static bool user_interactive() {return true;}
326 
329  static xtd::ustring user_name();
330 
333  static xtd::version version();
335 
337 
342  static void exit(int exit_code) {::_Exit(exit_code);}
343 
349  xtd::ustring buffer = name;
350  xtd::ustring result;
351 
352  size_t index = buffer.index_of('%');
353  while (index != xtd::ustring::npos && buffer.index_of('%', index + 1) != xtd::ustring::npos) {
354  result += buffer.substring(0, index);
355  buffer = buffer.remove(0, index + 1);
356  index = buffer.index_of('%');
357  if (get_environment_variable(buffer.substring(0, index)) != "")
358  result += get_environment_variable(buffer.substring(0, index));
359  else
360  result += xtd::ustring::format("%{0}%", buffer.substring(0, index));
361  buffer = buffer.remove(0, index + 1);
362  index = buffer.index_of('%');
363  }
364  result += buffer;
365  return result;
366  }
367 
383  static xtd::collections::specialized::string_vector get_command_line_args();
384 
392 
401  static xtd::ustring get_environment_variable(const xtd::ustring& variable, environment_variable_target target);
402 
406  static std::map<std::string, std::string>& get_environment_variables() {return get_environment_variables(environment_variable_target::process);}
407 
414  static std::map<std::string, std::string>& get_environment_variables(environment_variable_target target);
415 
421 
427  static xtd::ustring get_folder_path(environment::special_folder folder, environment::special_folder_option option);
428 
433  }
434 
441  static void set_environment_variable(const xtd::ustring& variable, const xtd::ustring& value) {
442  set_environment_variable(variable, value, environment_variable_target::process);
443  }
444 
453  static void set_environment_variable(const xtd::ustring& variable, const xtd::ustring& value, environment_variable_target target);
454  };
456 
458  inline std::ostream& operator<<(std::ostream& os, environment::special_folder value) {return os << to_string(value, {{environment::special_folder::desktop, "desktop"}, {environment::special_folder::programs, "programs"}, {environment::special_folder::my_documents, "my_documents"}, {environment::special_folder::personal, "personal"}, {environment::special_folder::favorites, "favorites"}, {environment::special_folder::startup, "startup"}, {environment::special_folder::recent, "recent"}, {environment::special_folder::send_to, "send_to"}, {environment::special_folder::start_menu, "start_menu"}, {environment::special_folder::personal, "my_music"}, {environment::special_folder::my_videos, "my_videos"}, {environment::special_folder::desktop_directory, "desktop_directory"}, {environment::special_folder::my_computer, "my_computer"}, {environment::special_folder::network_shortcuts, "network_shortcuts"}, {environment::special_folder::fonts, "fonts"}, {environment::special_folder::templates, "templates"}, {environment::special_folder::common_start_menu, "common_start_menu"}, {environment::special_folder::common_programs, "common_programs"}, {environment::special_folder::common_startup, "common_startup"}, {environment::special_folder::common_desktop_directory, "common_desktop_directory"}, {environment::special_folder::application_data, "application_data"}, {environment::special_folder::printer_shortcuts, "printer_shortcuts"}, {environment::special_folder::local_application_data, "local_application_data"}, {environment::special_folder::internet_cache, "internet_cache"}, {environment::special_folder::cookies, "cookies"}, {environment::special_folder::history, "history"}, {environment::special_folder::common_application_data, "common_application_data"}, {environment::special_folder::windows, "windows"}, {environment::special_folder::system, "system"}, {environment::special_folder::program_files, "program_files"}, {environment::special_folder::my_pictures, "my_pictures"}, {environment::special_folder::user_profile, "user_profile"}, {environment::special_folder::system_x86, "system_x86"}, {environment::special_folder::program_files_x86, "program_files_x86"}, {environment::special_folder::common_program_files, "common_program_files"}, {environment::special_folder::common_program_files_x86, "common_program_files_x86"}, {environment::special_folder::common_templates, "common_templates"}, {environment::special_folder::common_documents, "common_documents"}, {environment::special_folder::common_admin_tools, "common_admin_tools"}, {environment::special_folder::admin_tools, "admin_tools"}, {environment::special_folder::common_music, "common_music"}, {environment::special_folder::common_pictures, "common_pictures"}, {environment::special_folder::common_videos, "common_videos"}, {environment::special_folder::resources, "resources"}, {environment::special_folder::localized_resources, "localized_resources"}, {environment::special_folder::common_oem_links, "common_oem_links"}, {environment::special_folder::cd_burning, "cd_burning"}, {environment::special_folder::home, "home"}});}
459  inline std::wostream& operator<<(std::wostream& os, environment::special_folder value) {return os << to_string(value, {{environment::special_folder::desktop, L"desktop"}, {environment::special_folder::programs, L"programs"}, {environment::special_folder::my_documents, L"my_documents"}, {environment::special_folder::personal, L"personal"}, {environment::special_folder::favorites, L"favorites"}, {environment::special_folder::startup, L"startup"}, {environment::special_folder::recent, L"recent"}, {environment::special_folder::send_to, L"send_to"}, {environment::special_folder::start_menu, L"start_menu"}, {environment::special_folder::personal, L"my_music"}, {environment::special_folder::my_videos, L"my_videos"}, {environment::special_folder::desktop_directory, L"desktop_directory"}, {environment::special_folder::my_computer, L"my_computer"}, {environment::special_folder::network_shortcuts, L"network_shortcuts"}, {environment::special_folder::fonts, L"fonts"}, {environment::special_folder::templates, L"templates"}, {environment::special_folder::common_start_menu, L"common_start_menu"}, {environment::special_folder::common_programs, L"common_programs"}, {environment::special_folder::common_startup, L"common_startup"}, {environment::special_folder::common_desktop_directory, L"common_desktop_directory"}, {environment::special_folder::application_data, L"application_data"}, {environment::special_folder::printer_shortcuts, L"printer_shortcuts"}, {environment::special_folder::local_application_data, L"local_application_data"}, {environment::special_folder::internet_cache, L"internet_cache"}, {environment::special_folder::cookies, L"cookies"}, {environment::special_folder::history, L"history"}, {environment::special_folder::common_application_data, L"common_application_data"}, {environment::special_folder::windows, L"windows"}, {environment::special_folder::system, L"system"}, {environment::special_folder::program_files, L"program_files"}, {environment::special_folder::my_pictures, L"my_pictures"}, {environment::special_folder::user_profile, L"user_profile"}, {environment::special_folder::system_x86, L"system_x86"}, {environment::special_folder::program_files_x86, L"program_files_x86"}, {environment::special_folder::common_program_files, L"common_program_files"}, {environment::special_folder::common_program_files_x86, L"common_program_files_x86"}, {environment::special_folder::common_templates, L"common_templates"}, {environment::special_folder::common_documents, L"common_documents"}, {environment::special_folder::common_admin_tools, L"common_admin_tools"}, {environment::special_folder::admin_tools, L"admin_tools"}, {environment::special_folder::common_music, L"common_music"}, {environment::special_folder::common_pictures, L"common_pictures"}, {environment::special_folder::common_videos, L"common_videos"}, {environment::special_folder::resources, L"resources"}, {environment::special_folder::localized_resources, L"localized_resources"}, {environment::special_folder::common_oem_links, L"common_oem_links"}, {environment::special_folder::cd_burning, L"cd_burning"}, {environment::special_folder::home, L"home"}});}
460  inline std::ostream& operator<<(std::ostream& os, environment::special_folder_option value) {return os << to_string(value, {{environment::special_folder_option::none, "none"}, {environment::special_folder_option::do_not_verify, "do_not_verify"}, {environment::special_folder_option::create, "create"}});}
461  inline std::wostream& operator<<(std::wostream& os, environment::special_folder_option value) {return os << to_string(value, {{environment::special_folder_option::none, L"none"}, {environment::special_folder_option::do_not_verify, L"do_not_verify"}, {environment::special_folder_option::create, L"create"}});}
463 }
Contains xtd::diagnostics::stack_trace class.
The file system directory that contains the programs that appear in the Startup folder for all users...
The file system directory that serves as a repository for videos that belong to a user...
Contains xtd::static_object class.
ustring remove(size_t start_index) const noexcept
Deletes all the characters from this string beginning at a specified position and continuing through ...
A folder for components that are shared across applications. This special folder is valid only for Wi...
static xtd::ustring stack_trace()
Gets current stack trace information.
Definition: environment.h:299
Represents information about c++ libraries, such as the version and standard identifier. This class cannot be inherited.
Definition: compiler.h:23
Contains xtd::operating_system class.
The file system directory that contains resource data.
The environment variable is stored or retrieved from the environment block associated with the curren...
static xtd::cpp_language cpp_version()
Gets an cpp_standard object that contains the current c++ standard identifier and version number...
Definition: environment.h:222
The directory that contains the user&#39;s program groups.
static xtd::collections::specialized::string_vector get_logical_drives()
Returns an array of string containing the names of the logical drives on the current computer...
Definition: environment.h:431
The directory that serves as a common repository for documents. This member is equivalent to MyDocume...
special_folder
Specifies enumerated constants used to retrieve directory paths to system special folders...
Definition: environment.h:93
size_t index_of(value_type value) const noexcept
Reports the index of the first occurrence of the specified character in this string.
static bool is_64_bit_operating_system()
Determines whether the current operating system is a 64-bit operating system.
Definition: environment.h:260
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:38
The file system directory that contains the link objects that can exist in the Printers virtual folde...
The path to the folder is verified. If the folder exists, the path is returned. If the folder does no...
A file system directory that contains the link objects that may exist in the My Network Places virtua...
static xtd::ustring command_line()
Gets the command line for this process.
Definition: environment.h:211
static std::map< std::string, std::string > & get_environment_variables()
Retrieves all environment variable names and their values from the current process.
Definition: environment.h:406
A virtual folder that contains fonts.
Contains xtd::io::directory class.
Contains xtd::guid class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Represents a stack trace, which is an ordered collection of one or more stack frames.
Definition: stack_trace.h:37
The file system directory that acts as a staging area for files waiting to be written to a CD...
The directory used to physically store file objects on the desktop. Do not confuse this directory wit...
Contains xtd::environment_variable_target enum class.
The file system directory that serves as a repository for image files common to all users...
special_folder_option
Specifies options to use for getting the path to a special folder.
Definition: environment.h:194
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
Represents information about c++ libraries, such as the version and language identifier. This class cannot be inherited.
Definition: cpp_language.h:24
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition: version.h:93
std::string to_string(const date_time &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: date_time.h:1110
Represents information about an operating system, such as the version and platform identifier...
Definition: operating_system.h:23
The file system directory that serves as a repository for video files common to all users...
The file system directory that contains documents that are common to all users. This special folder i...
Contains xtd::compiler class.
ustring substring(size_t start_index) const noexcept
Retrieves a substring from this instance. The substring starts at a specified character position and ...
Indicates that no style elements, such as leading or trailing white space, thousands separators...
static bool user_interactive()
Gets a value indicating whether the current process is running in user interactive mode...
Definition: environment.h:325
The file system directory that contains home folder.
static uint32_t processor_count()
Gets the number of processors on the current machine.
Definition: environment.h:291
The directory that serves as a common repository for document templates.
The file system directory that contains the programs and folders that appear on the Start menu for al...
The file system directory that is used to store administrative tools for an individual user...
static xtd::ustring expand_environment_variables(const xtd::ustring &name)
Replaces the name of each environment variable embedded in the specified string with the string equiv...
Definition: environment.h:348
The directory that contains the Start menu items.
static std::vector< xtd::ustring > get_logical_drives()
Retrieves the names of the logical drives on this computer in the form "<drive letter>:\".
Contains xtd::collections::specialized::string_vector typedef.
Contains xtd::cpp_language class.
std::vector< xtd::ustring > string_vector
Represents a collection of strings.
Definition: string_vector.h:23
The My Computer folder. The MyComputer constant always yields the empty string ("") because no path i...
static xtd::ustring get_environment_variable(const xtd::ustring &variable)
Retrieves the value of an environment variable from the current process.
Definition: environment.h:391
static void exit(int exit_code)
Terminates this process and returns an exit code to the operating system.
Definition: environment.h:342
The Windows directory or SYSROOT. This corresponds to the windir% or SYSTEMROOT% environment variable...
The file system directory that contains administrative tools for all users of the computer...
The path to the folder is returned without verifying whether the path exists. If the folder is locate...
static xtd::ustring system_directory()
Gets the fully qualified path of the system directory.
Definition: environment.h:306
The file system directory that serves as a repository for music files common to all users...
The file system directory that contains files and folders that appear on the desktop for all users...
Contains xtd::processor class.
Contains xtd::ustring class.
The directory that serves as a common repository for the user&#39;s favorite items.
Contains xtd::version class.
The directory that serves as a common repository for Internet cookies.
The program files directory.On a non-x86 system, passing ProgramFiles to the GetFolderPath method ret...
static std::thread::id current_thread_id()
Gets a unique identifier for the current thread.
Definition: environment.h:243
The directory for components that are shared across applications.To get the x86 common program files ...
The directory that contains the Send To menu items.
The directory that corresponds to the user&#39;s Startup program group. The system starts these programs ...
The Program Files folder On an x86 system, passing the ProgramFilesX86 member to the Environment...
The logical Desktop rather than the physical file system location.
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition: ustring.h:708
static bool is_64_bit_process()
Determines whether the current process is a 64-bit process.
Definition: environment.h:264
xtd::ustring to_string() const noexcept override
Builds a readable representation of the stack trace.
static xtd::ustring get_folder_path(environment::special_folder folder)
Gets the path to the system special folder that is identified by the specified enumeration.
Definition: environment.h:420
The directory that serves as a common repository for application-specific data for the current roamin...
Represents information about a processor, such as the architecture. This class cannot be inherited...
Definition: processor.h:19
The directory that serves as a common repository for application-specific data that is used by all us...
environment_variable_target
Specifies the location where an environment variable is stored or retrieved in a set or get operation...
Definition: environment_variable_target.h:15
Contains xtd::argument_exception exception.
The path to the folder is created if it does not already exist.
The file system directory that contains localized resource data.
static std::basic_ostream< char_t, traits_t > & new_line(std::basic_ostream< char_t, traits_t > &os)
Inserts a new-line character and flushes the stream.
Definition: environment.h:279
static xtd::compiler compiler_version()
Gets an cpp_standard object that contains the current c++ standard identifier and version number...
Definition: environment.h:215
The user&#39;s profile folder. Applications should not create files or folders at this level; they should...
The file system directory that contains the templates that are available to all users. This special folder is valid only for Windows NT systems.
The environment class.
Definition: environment.h:34
The directory that contains the user&#39;s most recently used documents.
This value is recognized in Windows Vista for backward compatibility, but the special folder itself i...
static ustring join(const ustring separator, const collection_t &values) noexcept
Concatenates a specified separator string between each element of a specified object array...
Definition: ustring.h:869
The directory that serves as a common repository for application-specific data that is used by the cu...
static void set_environment_variable(const xtd::ustring &variable, const xtd::ustring &value)
Creates, modifies, or deletes an environment variable stored in the current process.
Definition: environment.h:441
The directory that serves as a common repository for temporary Internet files.
The My Documents folder. This member is equivalent to Personal.
Contains xtd::platform_id enum class.
The directory that serves as a common repository for Internet history items.