Environment - Leo-Corporation/LeoCorpLibrary GitHub Wiki

Table of content


Introduction

To use these methods and features, do not forget to put this line of code on top of your code file:

C#

using LeoCorpLibrary;

VB

Imports LeoCorpLibrary

Env

Methods

File System

a. GetFilesCount

This function is available in version 1.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The GetFilesCount() method allows you to get the number of files in a specified directory. Returns a int value.

Variation 1

Env.GetFilesCount(string directory) {...}
Value Argument Description
string directory Directory where you want to get the number of files

Variation 2

Env.GetFilesCount(string directory, bool includeSubDirectories) {...}
Value Argument Description
string directory Directory where you want to get the number of files
bool includeSubDirectories Count files that are located in the subdirectories

Here's an example of usage:

C#

// Without subdirectories
int fileNumber = Env.GetFilesCount(@"C:\Users");

// With subdirectories
int fileNumberWithSubDirectories = Env.GetFilesCount(@"C:\Users", true);

VB

' Without subdirectories
Dim fileNumber As Integer = Env.GetFilesCount("C:/Users")

' With subdirectories
Dim fileNumberWithSubDirectories As Integer = Env.GetFilesCount("C:/Users", True)

Go to top

b. GetDirectoriesCount

This function is available in version 1.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The GetDirectoryCount method allow you to get the number of directoires in a specified directory. Returns a int value. It has two variation

Variation 1

Env.GetDirectoriesCount(string directory) {...}
Value Argument Description
string directory Directory where you want to get the number of directories

Variation 2

Env.GetDirectoriesCount(string directory, bool includeSubDirectories) {...}
Value Argument Description
string directory Directory where you want to get the number of directories
bool includeSubDirectories Also include directories in subdirectories

Here's an example of usage:

C#

// Without subdirectories
int directoryNumber = Env.GetDirectoriesCount(@"C:\Users");

// With subdirectories
int directoryNumberWithSubDirectories = Env.GetDirectoriesCount(@"C:\Users", true);

VB

' Without subdirectories
Dim directoryNumber As Integer = Env.GetDirectoriesCount("C:/Users")

' With subdirectories
Dim directoryNumberWithSubDirectories As Integer = Env.GetDirectoriesCount("C:/Users", True)

Go to top

c. GetTotalDriveSpace

This function is available in version 1.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The GetTotalDriveSpace method allow you to get the total space of a specified volume. Returns a double value.

It's in:

LeoCorpLibrary.Env.GetTotalDriveSpace()

It has two arguments:

Value Argument Description Example
string drive Directory of the volume C:\
UnitType unitType Type of unit to return UnitType.Gigabyte

Here's an example of usage:

C#

double totalSpace = Env.GetTotalDriveSpace(@"C:\", UnitType.Gigabyte);

VB

Dim totalSpace As Double = Env.GetTotalDriveSpace("C:/", UnitType.Gigabyte)

Go to top

d. GetDriveAvailableFreeSpace

This function is available in version 1.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The GetDriveAvailableFreeSpace() method allows you to get the available space of specified volume. Returns a double value.

It's in:

LeoCorpLibrary.Env.GetDriveAvailableFreeSpace()

It has two arguments:

Value Argument Description Example
string drive Directory of the volume C:\
UnitType unitType Type of unit to return (MB, GB) UnitType.Gigabyte

Here's an example of usage:

C#

double freeSpace = Env.GetDriveAvailableFreeSpace(@"C:\", UnitType.Gigabyte);

VB

Dim freeSpace As Double = Env.GetDriveAvailableFreeSpace("C:/", UnitType.Gigabyte)

Go to top

e. GetOccupiedDriveSpace

This function is available in version 1.6 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The GetOccupiedDriveSpace() allows you to get the occupied drive space of specified volume. Returns a double value.

It's in:

LeoCorpLibrary.Env.GetOccupiedDriveSpace()

It has two arguments:

Value Argument Description Example
string drive Directory of the volume C:\
UnitType unitType Type of unit to return (MB, GB) UnitType.Gigabyte

Here's an example of usage:

C#

double occupiedSpace = GetOccupiedDriveSpace(@"C:\", UnitType.Gigabyte);

VB

Dim occupiedSpace As Double = GetOccupiedDriveSpace("C:/", UnitType.Gigabyte)

Go to top

f. CountFileCharacters

This function is available in version 2.1 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The CountFileCharacters() method enables you to get the number of characters in a specified file. It returns a int value.

It's in:

LeoCorpLibrary.Env.CountFileCharacters()

It has two arguments:

Value Argument Description
string fileName The file's location where you wanna count characters

Here's an example of usage:

C#

string filePath = @"C:\File.txt";
int characters = Env.CountFileCharacters(filePath);

VB

Dim filePath As String = "C:\\File.txt"
Dim characters As Integer = Env.CountFileCharacyers(filePath)

Go to top

g. CountFileCharactersAsync

This function is available in version 2.1 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The CountFileCharactersAsync() method enables you to count the number of characters in a file, asynchronously. It returns a Task<int> value.

It's in:

LeoCorpLibrary.Env.CountFileCharactersAsync()

It has one argument:

Value Argument Description
string fileName The file's location where you wanna count characters

Here's an example of usage:

C#

async void CountCharacters()
{
  string filePath = @"C:\File.txt";
  int count = await Env.CountFileCharactersAsync(filePath);
}

VB

Private Async Sub CountCharacters()
    Dim filePath As String = "C:\\File.txt"
    Dim count As Integer = Await Env.CountFileCharactersAsync(filePath)
End Sub

Go to top

h. IsDirectoryHasPermission

This function is available in version 3.7 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The IsDirectoryHasPermission() method allows you to see if your program has permissions to write files/directories to a specified directory. It returns a bool value.

It's in:

LeoCorpLibrary.Env.IsDirectoryHasPermission()

It has one argument:

Value Argument Description
string filePath The path to the directory

Here's an example of usage:

C#

if (Env.IsDirectoryHasPermission("C:/Windows"))
{
    File.Create("C:/Windows/file.txt");
}

VB

If Env.IsDirectoryHasPermission("C:/Windows") Then
    File.Create("C:/Windows/file.txt")
End If

Go to top

System Environment

a. GetWindowsVersion

This function is available in version 1.9 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The GetWindowsVersion() enables you to get the Windows version that the user is running. Returns a WindowsVersion value.

It's in:

LeoCorpLibrary.Env.GetWindowsVersion()

Warning: This method works only if you specify in the Application Manifest, in the compatibilty section, that Windows 8, 8.1 and 10 are compatible with the software. Else, it will return Windows8.

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- Windows Vista -->
      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

      <!-- Windows 7 -->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />

      <!-- Windows 8 -->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />

      <!-- Windows 8.1 -->
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />

      <!-- Windows 10 -->
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

    </application>
  </compatibility>

This file is an example. You can create this file by right-clicking on the project, then "Add", "New element", then "Application manifest" and "Add".

Here's an example of usage:

C#

WindowsVersion winver = Env.GetWindowsVersion(); // Obtenir la version
/*
Can return:
WindowsVersion.Windows7
WindowsVersion.Windows8
WindowsVersion.Windows81
WindowsVersion.Windows10
/*

VB

Dim winver As WindowsVersion = Env.GetWindowsVersion() ' Obtenir la version
' Can return:
' WindowsVersion.Windows7
' WindowsVersion.Windows8
' WindowsVersion.Windows81
' WindowsVersion.Windows10

Go to top

b. ExecuteAsAdmin

This function is available in version 1.9 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The ExecuteAsAdmin() allows you to execute a program in admin mode on Windows.

It's in:

LeoCorpLibrary.Env.ExecuteAsAdmin()

This method has two variation:

Variation 1

Env.ExecuteAsAdmin(Process process) {...}
Value Argument Description
Process process Process with StartInfo.FileName indicated

Variation 2

Env.ExecuteAsAdmin(string filename) {...}
Value Argument Description
string filename The location of the sofware to launch in admin mode

Here's an example of usage:

C#

// Variation 1
Process process = new Process();
process.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
Env.ExecuteAsAdmin(process);

// Variation 2
Env.ExecuteAsAdmin(@"C:\Windows\System32\cmd.exe");

VB

' Variation 1
Dim process As Process = New Process()
process.StartInfo.FileName = "C:/Windows/System32/cmd.exe"
Env.ExecuteAsAdmin(process)

' Variation 2
Env.ExecuteAsAdmin("C:/Windows/System32/cmd.exe")

Go to top

c. GetUnixTime

This function is available in version 2.1 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to get the curent UnixTime or the UnixTime of a specific date/time. Returns a int value.

It's in:

LeoCorpLibrary.Env.GetUnixTime()

It has two variation:

Variation 1

The variation does not accept any arguments.

Variation 2

This variation has an argument:

Value Argument Description
DateTime date The specific date to get the UnixTime of

Here's an example of usage:

C#

// Variation 1
int curentUnixTime = Env.GetUnixTime();

// Variation 2
DateTime dateTime = new DateTime(1970, 1, 1);
int unixTime = Env.GetUnixTime(dateTime);

VB

' Variation 1
Dim curentUnixTime As Integer = Env.GetUnixTime()

' Variation 2
Dim dateTime As DateTime = New DateTime(1970, 1, 1)
Dim unixTime As Integer = Env.GetUnixTime(dateTime)

Go to top

d. GetAppDataPath

This function is available in version 2.2 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to get the user's %APPDATA% path. Returns a string value.

It's in:

LeoCorpLibrary.Env.GetAppDataPath()

It has no arguments.

Here's an example of usage:

C#

string appDataPath = Env.GetAppDataPath();

VB

dim appDataPath As String = Env.GetAppDataPath()

Go to top

e. GetMouseCursorPosition

This function is available in version 3.4 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to get the current position of the mouse cursor on the screen. It returns a Point value (System.Drawing).

It's in:

LeoCorpLibrary.Env.GetMouseCursorPosition()

It has no arguments.

Note: This method is using Windows Forms features, so use this method in Windows Forms projects only. If your project is using WPF, check GetMouseCursorPositionWPF().

Here's an example of usage:

C#

Point p = Env.GetMouseCursorPosition();
MessageBox.Show($"Mouse position: X = {p.X}; Y = {p.Y}");

VB

Dim p As Point = Env.GetMouseCursorPosition()
MessageBox.Show("Mouse position: X = " + p.X.ToString() + "; Y = " + p.Y.ToString())

Go to top

f. GetMouseCursorPositionWPF

This function is available in version 3.4 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to get the current position of the mouse cursor on the screen. It returns a Point value (System.Windows).

It's in:

LeoCorpLibrary.Env.GetMouseCursorPositionWPF()

It has no arguments.

Here's an example of usage:

C#

Point p = Env.GetMouseCursorPosition();
MessageBox.Show($"Mouse position: X = {p.X}; Y = {p.Y}");

VB

Dim p As Point = Env.GetMouseCursorPosition()
MessageBox.Show("Mouse position: X = " + p.X.ToString() + "; Y = " + p.Y.ToString())

Go to top

g. UnixTimeToDateTime

This function is available in version 3.4 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to convert UnixTime (int) to a DateTime object. Returns a DateTime value.

It's in:

LeoCorpLibrary.Env.UnixTimeToDateTime()

It has one argument:

Value Argument Description
int unixTime The UnixTime to convert

Here's an example of usage:

C#

int unixTime = 1615538407;
DateTime date = Env.UnixTimeToDateTime(unixTime); // 12/03/2021 @ 09:40:07

VB

Dim unixTime As Integer = 1615538407
Dim date As DateTime = Env.UnixTimeToDateTime(unixTime) ' 12/03/2021 @ 09:40:07

Go to top

h. IsProcessRunning

This function is available in version 3.4 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to check if a specific process is currently running. Returns a bool value.

It's in:

LeoCorpLibrary.Env.IsProcessRunning()

It has one argument:

Value Argument Description
int processName The process name to find

Here's an example of usage:

C#

if (IsProcessRunning("cmd.exe"))
{
    MessageBox.Show("You have a command prompt running.")
}

VB

If IsProcessRunning("cmd.exe") Then
    MessageBox.Show("You have a command prompt running.")
End If

Go to top

i. LaunchUWPApp

This function is available in version 3.9 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

This method allows you to launch an UWP app from its PackageFamilyName and its Application Id. You can find these informations by running in Windows Powershell (admin) the following command:

Get-AppxPackage | Select PackageFamilyName, InstallLocation

You can find the Application Id in the AppxManifest.xml file in

InstallLocation\AppxManifest.xml

This method isn't available in LeoCorpLibrary.Core.

It's in:

LeoCorpLibrary.Env.LaunchUWPApp()

It has two arguments:

Value Argument Description
string packageFamilyName The PackageFamilyName property
string applicationID The Application Id property in the UWP AppxManifest.xml file

Here's an example of usage:

C#

// Launch Minecraft UWP
Env.LaunchUWPApp("Microsoft.MinecraftUWP_8wekyb3d8bbwe", "App"); // Launch

VB

' Launch Minecraft UWP
Env.LaunchUWPApp("Microsoft.MinecraftUWP_8wekyb3d8bbwe", "App") ' Launch

Go to top

Properties

File System

a. SystemDrive

This property is available in version 3.1 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The SystemDrive property allows you to get the DriveInfo of the system drive.

Warning: This property only works on Windows!

It's in:

LeoCorpLibrary.Env.SystemDrive

Here's an example of usage:

C#

DriveInfo sysDrive = Env.SystemDrive; // Get system drive

VB

Dim sysDrive As DriveInfo = Env.SystemDrive ' Get system drive

Go to top

b. AppDataPath

This property is available in version 3.1 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The AppDataPath property allows you to get the %APPDATA% directory path.

It's in:

LeoCorpLibrary.Env.AppDataPath

Here's an example of usage:

C#

string appDataPath = Env.AppDataPath; // Get %APPDATA%

VB

Dim appDataPath As String = Env.AppDataPath ' Get %APPDATA%

Go to top

System Environment

a. CurrentOperatingSystem

This property is available in version 3.1 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The CurrentOperatingSystem property allows you to get the current Operating system. It returns a OperatingSystems enum:

  • Windows
  • macOS
  • Linux
  • Unknown

It's in:

LeoCorpLibrary.Env.CurrentOperatingSystem

Here's an example of usage:

C#

Console.WriteLine($"The current OS is {Env.CurrentOperatingSystem.ToString()}");

VB

Console.WriteLine("The current OS is" + Env.CurrentOperatingSystem.ToString())

Go to top

b. UnixTime

This property is available in version 3.1 and higher.

Compatibility
Framework LeoCorpLibrary LeoCorpLibrary.Core
.NET 5
.NET Core 3.1
.NET Framework 4.7.2
.NET Framework 4.5

The UnixTime property allows you to get the current Unix time.

It's in:

LeoCorpLibrary.Env.UnixTime

Here's an example of usage:

C#

while (true)
{
    Console.WriteLine(Env.UnixTime.ToString());
    Thread.Sleep(1000);
}

VB

While True
    Console.WriteLine(Env.UnixTime.ToString())
    Thread.Sleep(1000)
End While

Go to top

⚠️ **GitHub.com Fallback** ⚠️