重庆熊猫 Loading

.NET中获取Windows的常见路径

更新记录
本文迁移自Panda666原博客,原发布时间:2021年6月30日。

一、说明

每种平台都其预定义的规则,比如Windows平台有自己的规定、公司也有预定义的规章制度。那么在平台上开发和干活,就要遵守人家的约定进行工作。当然,叛逆的你也可以不按约定出牌。常用Windows系统的小伙伴,通过GUI操作系统常用的路径基本大家都熟悉。下面通过编程的方式获得系统的常见路径。

二、使用Environment类型

直接Environment静态类型的GetFolderPath()方法即可获得许多预定义的Windows常见路径。

通过在Visual Studio中F12查看类型,可以看到详细的枚举定义。

实例具体如下:

//获得桌面所在的路径(虚拟)
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
//获得桌面所在的路径(实际)
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
//获得启动下的应用程序目录
Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.Programs)
//获得我的文档路径
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
//获得我的文档路径
Environment.GetFolderPath(Environment.SpecialFolder.Personal)
//获得我最喜欢路径
Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
//获得开启启动程序存放的目录
Environment.GetFolderPath(Environment.SpecialFolder.Startup)
//获得开启启动程序存放的目录(所有用户可见)
Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup)
//获得用户最近使用的目录
Environment.GetFolderPath(Environment.SpecialFolder.Recent)
//获得菜单项目录
Environment.GetFolderPath(Environment.SpecialFolder.SendTo)
//获得开始菜单目录
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)
//获得开始菜单上的目录(所有用户可见)
Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu)
//获得我的音乐目录
Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)
//获得我的视频目录
Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)
//我的图片所在目录
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
//获得网络虚拟文件夹所在目录
Environment.GetFolderPath(Environment.SpecialFolder.NetworkShortcuts)
//获得字体所在目录
Environment.GetFolderPath(Environment.SpecialFolder.Fonts)
//获得模板目录
Environment.GetFolderPath(Environment.SpecialFolder.Templates)
//获得模板文件目录(所有用户可见)
Environment.GetFolderPath(Environment.SpecialFolder.CommonTemplates)
//获得桌面所在目录(所有用户可见)
Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
//获得应用数据所在目录(带同步)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
//获得应用数据所在目录(本地用户)
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
//获得[internet]网络临时文件所在目录
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
//获得[internet]网络Cookie所在目录
Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
//获得[internet]浏览历史记录
Environment.GetFolderPath(Environment.SpecialFolder.History)
//获得应用数据所在目录
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
//Windows所在目录
//%windir% or %SYSTEMROOT%
Environment.GetFolderPath(Environment.SpecialFolder.Windows)
//System所在目录
Environment.GetFolderPath(Environment.SpecialFolder.System)
//System所在目录(x86)
Environment.GetFolderPath(Environment.SpecialFolder.SystemX86)
//Program Files所在目录
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
//Program Files所在目录(x86)
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
//当前用户配置文件顶层所在目录
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
//应用程序共享文件所在目录
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles)
//应用程序共享文件所在目录(x86)
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86)
//公用文档目录
Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments)
//公用音乐目录
Environment.GetFolderPath(Environment.SpecialFolder.CommonMusic)
//公用图片目录
Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures)
//公用视频目录
Environment.GetFolderPath(Environment.SpecialFolder.CommonVideos)
posted @ 2022-04-16 16:29  重庆熊猫  阅读(184)  评论(0编辑  收藏  举报