如何在 C# 中获取当前用户的桌面路径?
SpecialDirectories
,它具有以下属性:
My.Computer.FileSystem.SpecialDirectories.Desktop
如何在 C# 中执行此操作?
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// Environment.GetFolderPath
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); // Current User's Application Data
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); // All User's Application Data
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles); // Program Files
Environment.GetFolderPath(Environment.SpecialFolder.Cookies); // Internet Cookie
Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Logical Desktop
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); // Physical Desktop
Environment.GetFolderPath(Environment.SpecialFolder.Favorites); // Favorites
Environment.GetFolderPath(Environment.SpecialFolder.History); // Internet History
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); // Internet Cache
Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); // "My Computer" Folder
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // "My Documents" Folder
Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); // "My Music" Folder
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); // "My Pictures" Folder
Environment.GetFolderPath(Environment.SpecialFolder.Personal); // "My Document" Folder
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); // Program files Folder
Environment.GetFolderPath(Environment.SpecialFolder.Programs); // Programs Folder
Environment.GetFolderPath(Environment.SpecialFolder.Recent); // Recent Folder
Environment.GetFolderPath(Environment.SpecialFolder.SendTo); // "Sent to" Folder
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu); // Start Menu
Environment.GetFolderPath(Environment.SpecialFolder.Startup); // Startup
Environment.GetFolderPath(Environment.SpecialFolder.System); // System Folder
Environment.GetFolderPath(Environment.SpecialFolder.Templates); // Document Templates
此代码已验证与.NET 8兼容。更好的注释。感谢@David Buck
// Gets the path to the current user's Application Data folder.
// Example: C:\Users\[YourUserName]\AppData\Roaming
string applicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
// Gets the path to the Application Data folder for all users.
// Example: C:\ProgramData
string commonApplicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
// Gets the path to the Program Files folder (non-x86).
// Example: C:\Program Files
string programFilesPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
// Gets the path to the Internet Cookie folder.
// Example: C:\Users\[YourUserName]\AppData\Roaming\Microsoft\Windows\Cookies
string cookiesPath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
// Gets the path to the logical Desktop folder.
// Example: C:\Users\[YourUserName]\Desktop
// In modern Windows, this is usually the same as DesktopDirectory.
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// Gets the path to the physical Desktop folder.
// Example: C:\Users\[YourUserName]\Desktop
// In modern Windows, this is usually the same as Desktop.
string desktopDirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
// Gets the path to the Favorites folder.
// Example: C:\Users\[YourUserName]\Favorites
string favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
// Gets the path to the Internet History folder.
// Example: C:\Users\[YourUserName]\AppData\Local\Microsoft\Windows\History
string historyPath = Environment.GetFolderPath(Environment.SpecialFolder.History);
// Gets the path to the Internet Cache folder.
// Example: C:\Users\[YourUserName]\AppData\Local\Microsoft\Windows\INetCache
string internetCachePath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
// Gets the path to the "My Documents" folder (same as Personal).
// Example: C:\Users\[YourUserName]\Documents
string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Gets the path to the "My Music" folder.
// Example: C:\Users\[YourUserName]\Music
string myMusicPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
// Gets the path to the "My Pictures" folder.
// Example: C:\Users\[YourUserName]\Pictures
string myPicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
// Gets the path to the "My Documents" folder (same as MyDocuments).
// Example: C:\Users\[YourUserName]\Documents
string personalPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
// Gets the path to the Programs folder.
// Example: C:\Users\[YourUserName]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
string programsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
// Gets the path to the Recent folder.
// Example: C:\Users\[YourUserName]\AppData\Roaming\Microsoft\Windows\Recent
string recentPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent);
// Gets the path to the "Send To" folder.
// Example: C:\Users\[YourUserName]\AppData\Roaming\Microsoft\Windows\SendTo
string sendToPath = Environment.GetFolderPath(Environment.SpecialFolder.SendTo);
// Gets the path to the Start Menu folder.
// Example: C:\Users\[YourUserName]\AppData\Roaming\Microsoft\Windows\Start Menu
string startMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
// Gets the path to the Startup folder.
// Example: C:\Users\[YourUserName]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
// Gets the path to the System folder.
// Example: C:\Windows\System32
string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
// Gets the path to the Document Templates folder.
// Example: C:\Users\[YourUserName]\Documents\Custom Office Templates
string templatesPath = Environment.GetFolderPath(Environment.SpecialFolder.Templates);