Đường dẫn Desktop của user hiện tại có thể lấy bằng Environment.SpecialFolder. Environment.SpecialFolder lấy đường dẫn đến system special folder được xác định bởi enumeration chỉ định.
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Lớp System.Environment cung cấp thông tin về môi trường và platform hiện tại. Dùng để lấy Environment variable settings, Version của common language runtime, nội dung call stack, v.v. Lớp này không thể kế thừa.
Environment class là static class cung cấp system configuration, current program execution environment và một số properties cho string manipulation như new line. Namespace System đại diện cho Environment Class.
Environment class là kết hợp functions và Properties đại diện cho Environment variable details dùng IDictionary dạng key,value pairs.
Có thể loop qua Dictionary để lấy tất cả Environment variable details.
Ví dụ
using System;
namespace DemoApplication
{
public class Program
{
public static void Main()
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine($"Đường dẫn Desktop: {desktopPath}");
Console.ReadLine();
}
}
}
Kết quả
Đường dẫn Desktop: C:\Users\UserName\Desktop