@manhng

Welcome to my blog!

Folder Explorer (C#)

July 14, 2020 13:42

Folder Explorer (C#) (edit)

What Are System Environment Variables?

Environment variables are strings that save information about the entire environment in your system. These string values are dynamic and they can affect the way your system will behave on. Environment variables can be classified into two main types:

System Variables: They affect the entire system whatever the current user is. They are defined by Windows and saved in the registry. You need to be an administrator to be able to modify them. You usually need to restart your computer to make these changes effective.

User Variables: They affect the current environment of the current system user. They can be deleted, modified, and added by any system user. They are used by Windows setup, by some programs, and by users. Changes to these variables are saved to the registry and be effective immediately.

Program.cs

using System;
using System.Linq;
using System.Security.Principal;

class Program
{
private static void Main(string[] args)
{
Console.WriteLine("%UserName%: " + Environment.GetEnvironmentVariable("UserName"));

//Getting machine and user information
Console.WriteLine("Machine Information");
Console.WriteLine("======================");
Console.WriteLine("Machine Name: " + Environment.MachineName);
Console.WriteLine("OS Version: " + Environment.OSVersion);
Console.WriteLine("System Directory: " + Environment.SystemDirectory);
Console.WriteLine("User Name: " + Environment.UserName);
Console.WriteLine("Version: " + Environment.Version);
Console.WriteLine("Current Directory: " + Environment.CurrentDirectory);
Console.WriteLine();

// Get all logical hard drives
string[] drives = Environment.GetLogicalDrives();
Console.WriteLine("======================");
Console.WriteLine("Available drives:");
foreach (string drive in drives)
{
Console.WriteLine(drive);
}

Console.WriteLine("%APPDATA%: " + System.Environment.ExpandEnvironmentVariables("%APPDATA%"));
Console.WriteLine("%PROGRAMDATA%: " + System.Environment.ExpandEnvironmentVariables("%PROGRAMDATA%"));

//Local Environment Variables
Console.WriteLine("%USERNAME%: " + System.Environment.ExpandEnvironmentVariables("%USERNAME%"));
Console.WriteLine("%USERPROFILE%: " + System.Environment.ExpandEnvironmentVariables("%USERPROFILE%"));
Console.WriteLine("%ALLUSERSPROFILE%: " + System.Environment.ExpandEnvironmentVariables("%ALLUSERSPROFILE%"));
Console.WriteLine("%APPDATA%: " + System.Environment.ExpandEnvironmentVariables("%APPDATA%"));
Console.WriteLine("%TEMP%: " + System.Environment.ExpandEnvironmentVariables("%TEMP%"));
Console.WriteLine("%TMP%: " + System.Environment.ExpandEnvironmentVariables("%TMP%"));

//System Environment Variables
Console.WriteLine("%COMPUTERNAME%: " + System.Environment.ExpandEnvironmentVariables("%COMPUTERNAME%"));
Console.WriteLine("%OS%: " + System.Environment.ExpandEnvironmentVariables("%OS%"));
Console.WriteLine("%PATH%: " + System.Environment.ExpandEnvironmentVariables("%PATH%"));
Console.WriteLine("%HOMEPATH%: " + System.Environment.ExpandEnvironmentVariables("%HOMEPATH%"));
Console.WriteLine("%SYSTEMDRIVE%: " + System.Environment.ExpandEnvironmentVariables("%SYSTEMDRIVE%"));
Console.WriteLine("%SYSTEMROOT%: " + System.Environment.ExpandEnvironmentVariables("%SYSTEMROOT%"));
Console.WriteLine("%WINDIR%: " + System.Environment.ExpandEnvironmentVariables("%WINDIR%"));

//Others
Console.WriteLine("%ALLUSERSPROFILE%: " + System.Environment.ExpandEnvironmentVariables("%ALLUSERSPROFILE%"));
Console.WriteLine("%CommonProgramFiles%: " + System.Environment.ExpandEnvironmentVariables("%CommonProgramFiles%"));
Console.WriteLine("%CommonProgramFiles(x86)%: " + System.Environment.ExpandEnvironmentVariables("%CommonProgramFiles(x86)%"));
Console.WriteLine("%CommonProgramW6432%: " + System.Environment.ExpandEnvironmentVariables("%CommonProgramW6432%"));

Console.WriteLine("%ProgramFiles%: " + System.Environment.ExpandEnvironmentVariables("%ProgramFiles%"));
Console.WriteLine("%CommonProgramFiles%: " + System.Environment.ExpandEnvironmentVariables("%CommonProgramFiles%"));

string ServerPath = "My Software";
if (System.Environment.Is64BitOperatingSystem)
{
//Is64Bit
ServerPath = System.IO.Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), ServerPath);
}
else
{
//Is32Bit
ServerPath = System.IO.Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles%"), ServerPath);
}

Console.WriteLine("My Software: " + ServerPath);

Console.WriteLine("Path.AltDirectorySeparatorChar: " + string.Format("'{0}'", System.IO.Path.AltDirectorySeparatorChar)); // => "/"
Console.WriteLine("Path.DirectorySeparatorChar: " + string.Format("'{0}'", System.IO.Path.DirectorySeparatorChar)); // => "\"
Console.WriteLine("Path.PathSeparator: " + string.Format("'{0}'", System.IO.Path.PathSeparator)); // => ";"
Console.WriteLine("Path.VolumeSeparatorChar: " + string.Format("'{0}'", System.IO.Path.VolumeSeparatorChar)); // => ":"

string[] ServerPath_Folders = ServerPath.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); // is safer for path
for (int i = 0; i < ServerPath_Folders.Length; i++)
{
Console.WriteLine(string.Format("ServerPath_Folders[{0}]: ", i) + ServerPath_Folders[i]);
}

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

if (windowsIdentity == null)
{
throw new InvalidOperationException("WindowsIdentity is null");
}

string domainName = windowsIdentity.Name.Split(System.IO.Path.DirectorySeparatorChar).First();
string nameWithoutDomain = windowsIdentity.Name.Split(System.IO.Path.DirectorySeparatorChar).Last();

Console.WriteLine("Domain Name: " + domainName);
Console.WriteLine("Name With Domain: " + windowsIdentity.Name);
Console.WriteLine("Name Without Domain: " + nameWithoutDomain);

Console.ReadLine();
}
}

Categories