如何在C#中获取当前用户的桌面路径?
我唯一能找到的就是VB.NET-only类SpecialDirectories
,它具有以下属性:
My.Computer.FileSystem.SpecialDirectories.Desktop
如何在C#中做到这一点?
如何在C#中获取当前用户的桌面路径?
我唯一能找到的就是VB.NET-only类SpecialDirectories
,它具有以下属性:
My.Computer.FileSystem.SpecialDirectories.Desktop
如何在C#中做到这一点?
Answers:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string extension = ".log";
filePath += @"\Error Log\" + extension;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
Directory.CreateDirectory
在创建目录之前已经检查了目录是否存在,因此您的if
语句是多余的。不知道此功能是否来自C#的更高版本,但我想提到它。
// 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