在本文中,我们展示了如何使用C#工作环境和平台。
Environment
类型提供有关C#中当前环境和平台的信息。它位于System
命名空间中。
C#系统信息
在下面的例子中我们打印一些系统信息。
var osv = Environment.OSVersion; Console.WriteLine($"OS: {osv.Platform}"); Console.WriteLine($"Version: {osv.Version}"); Console.WriteLine($"OS & version: {osv.VersionString}"); string userName = Environment.UserName; string userDomName = Environment.UserDomainName; Console.WriteLine($"User name: {userName}"); Console.WriteLine($"User domain name: {userDomName}");
该示例打印操作系统名称和版本、用户名和用户域名。
var osv = Environment.OSVersion;
Environment.OsVersion
属性获取当前平台标识符和版本号。
$ dotnet run OS: Unix Version: 5.15.0.48 OS & version: Unix 5.15.0.48 User name: jano User domain name: andromeda
C#通用目录
在下一个例子中,我们打印一些常见的系统目录。
string curDir = Environment.CurrentDirectory; string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string myMusicDir = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); string addDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string userProfDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string myPictDir = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); Console.WriteLine($"Current directory: {curDir}"); Console.WriteLine($"Desktop directory: {deskDir}"); Console.WriteLine($"My Music directory: {myMusicDir}"); Console.WriteLine($"Application data directory: {addDataDir}"); Console.WriteLine($"User profile directory: {userProfDir}"); Console.WriteLine($"My Pictures directory: {myPictDir}");
该示例显示了一些常用目录,包括桌面、我的音乐、应用程序数据、用户配置文件和我的图片。
$ dotnet run Current directory: /home/jano/Documents/prog/csharp/environment/EnvironmentEx Desktop directory: /home/jano/Desktop My Music directory: /home/jano/Music Application data directory: /home/jano/.config User profile directory: /home/jano My Pictures directory: /home/jano/Pictures
C#命令行参数
下一个示例打印命令行参数。
string cline = Environment.CommandLine; Console.WriteLine(cline); string[] cargs = Environment.GetCommandLineArgs(); foreach (var arg in cargs) { Console.WriteLine(arg); }
Environment.CommandLine
返回一串命令线性参数。要获取参数数组,我们使用Environment.GetCommandLineArgs
方法。
$ dotnet run "John Doe" gardener 34 /home/jano/Documents/prog/csharp/environment/EnvironmentEx/bin/Debug/net6.0/EnvironmentEx.dll "John Doe" gardener 34 /home/jano/Documents/prog/csharp/environment/EnvironmentEx/bin/Debug/net6.0/EnvironmentEx.dll John Doe gardener 34
C#环境变量
我们还可以检索环境变量。
using System.Collections; string? user = Environment.GetEnvironmentVariable("USER"); Console.WriteLine(user); string? shell = Environment.GetEnvironmentVariable("SHELL"); Console.WriteLine(shell); string? home = Environment.GetEnvironmentVariable("HOME"); Console.WriteLine(home); var envs = Environment.GetEnvironmentVariables(); foreach (DictionaryEntry de in envs) { Console.WriteLine($"{de.Key}: {de.Value}"); }
我们可以使用GetEnvironmentVariable
获取特定的环境变量。要检索所有变量,我们使用GetEnvironmentVariables
。
$ dotnet run jano /bin/bash /home/jano CLUTTER_IM_MODULE: ibus SDKMAN_VERSION: 5.16.0 PANEL_GDK_CORE_DEVICE_EVENTS: 0 MICRONAUT_HOME: /home/jano/.sdkman/candidates/micronaut/current LC_PAPER: sk_SK.UTF-8 ...
在本文中,我们使用了C#中的Environment
。
列出所有C#教程。