概述
由于软件开发的需要,近期对Windows特殊文件夹(如桌面,我的文档等)路径的查找方法进行了研究,结果如下。
获取特殊文件夹的方法不止一种,下面列出常用的几个方式。
一、使用 Environment.SpecialFolder 与 Enviroment.GetFolderPath()
简单直接的获取办法,能够获取系统自带的特殊文件夹的路径,而用户在环境变量中定义的路径(如配置Java添加的ClassPath等)不能查找。
这里举例说明如何使用这种方式:
string dir = Enviroment.GetFolderPath(Enviroment.SpecialFolder.Desktop);
这段代码执行后,dir的值就会变为本地系统的桌面文件夹路径。
下面这段代码枚举出了所有可以被这种方法获取的特殊文件夹及其路径:
foreach (Environment.SpecialFolder s in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
Console.WriteLine("{0} folder = {1}", s, Environment.GetFolderPath(s));
}
{
Console.WriteLine("{0} folder = {1}", s, Environment.GetFolderPath(s));
}
在个人计算机中(64位 Windows10系统)运行结果如下:
Desktop folder = C:UsersCYMDesktop
Programs folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart MenuPrograms
MyDocuments folder = C:UsersCYMDocuments
MyDocuments folder = C:UsersCYMDocuments
Favorites folder = C:UsersCYMFavorites
Startup folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart MenuProgramsStartup
Recent folder = C:UsersCYMAppDataRoamingMicrosoftWindowsRecent
SendTo folder = C:UsersCYMAppDataRoamingMicrosoftWindowsSendTo
StartMenu folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart Menu
MyMusic folder = C:UsersCYMMusic
MyVideos folder = C:UsersCYMVideos
DesktopDirectory folder = C:UsersCYMDesktop
MyComputer folder =
NetworkShortcuts folder = C:UsersCYMAppDataRoamingMicrosoftWindowsNetwork Shortcuts
Fonts folder = C:WindowsFonts
Templates folder = C:UsersCYMAppDataRoamingMicrosoftWindowsTemplates
CommonStartMenu folder = C:ProgramDataMicrosoftWindowsStart Menu
CommonPrograms folder = C:ProgramDataMicrosoftWindowsStart MenuPrograms
CommonStartup folder = C:ProgramDataMicrosoftWindowsStart MenuProgramsStartup
CommonDesktopDirectory folder = C:UsersPublicDesktop
ApplicationData folder = C:UsersCYMAppDataRoaming
PrinterShortcuts folder = C:UsersCYMAppDataRoamingMicrosoftWindowsPrinter Shortcuts
LocalApplicationData folder = C:UsersCYMAppDataLocal
InternetCache folder = C:UsersCYMAppDataLocalMicrosoftWindowsINetCache
Cookies folder = C:UsersCYMAppDataLocalMicrosoftWindowsINetCookies
History folder = C:UsersCYMAppDataLocalMicrosoftWindowsHistory
CommonApplicationData folder = C:ProgramData
Windows folder = C:Windows
System folder = C:Windowssystem32
ProgramFiles folder = C:Program Files (x86)
MyPictures folder = C:UsersCYMPictures
UserProfile folder = C:UsersCYM
SystemX86 folder = C:WindowsSysWOW64
ProgramFilesX86 folder = C:Program Files (x86)
CommonProgramFiles folder = C:Program Files (x86)Common Files
CommonProgramFilesX86 folder = C:Program Files (x86)Common Files
CommonTemplates folder = C:ProgramDataMicrosoftWindowsTemplates
CommonDocuments folder = C:UsersPublicDocuments
CommonAdminTools folder = C:ProgramDataMicrosoftWindowsStart MenuProgramsAdministrative Tools
AdminTools folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart MenuProgramsAdministrative Tools
CommonMusic folder = C:UsersPublicMusic
CommonPictures folder = C:UsersPublicPictures
CommonVideos folder = C:UsersPublicVideos
Resources folder = C:Windowsresources
LocalizedResources folder =
CommonOemLinks folder =
CDBurning folder = C:UsersCYMAppDataLocalMicrosoftWindowsBurnBurn
Programs folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart MenuPrograms
MyDocuments folder = C:UsersCYMDocuments
MyDocuments folder = C:UsersCYMDocuments
Favorites folder = C:UsersCYMFavorites
Startup folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart MenuProgramsStartup
Recent folder = C:UsersCYMAppDataRoamingMicrosoftWindowsRecent
SendTo folder = C:UsersCYMAppDataRoamingMicrosoftWindowsSendTo
StartMenu folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart Menu
MyMusic folder = C:UsersCYMMusic
MyVideos folder = C:UsersCYMVideos
DesktopDirectory folder = C:UsersCYMDesktop
MyComputer folder =
NetworkShortcuts folder = C:UsersCYMAppDataRoamingMicrosoftWindowsNetwork Shortcuts
Fonts folder = C:WindowsFonts
Templates folder = C:UsersCYMAppDataRoamingMicrosoftWindowsTemplates
CommonStartMenu folder = C:ProgramDataMicrosoftWindowsStart Menu
CommonPrograms folder = C:ProgramDataMicrosoftWindowsStart MenuPrograms
CommonStartup folder = C:ProgramDataMicrosoftWindowsStart MenuProgramsStartup
CommonDesktopDirectory folder = C:UsersPublicDesktop
ApplicationData folder = C:UsersCYMAppDataRoaming
PrinterShortcuts folder = C:UsersCYMAppDataRoamingMicrosoftWindowsPrinter Shortcuts
LocalApplicationData folder = C:UsersCYMAppDataLocal
InternetCache folder = C:UsersCYMAppDataLocalMicrosoftWindowsINetCache
Cookies folder = C:UsersCYMAppDataLocalMicrosoftWindowsINetCookies
History folder = C:UsersCYMAppDataLocalMicrosoftWindowsHistory
CommonApplicationData folder = C:ProgramData
Windows folder = C:Windows
System folder = C:Windowssystem32
ProgramFiles folder = C:Program Files (x86)
MyPictures folder = C:UsersCYMPictures
UserProfile folder = C:UsersCYM
SystemX86 folder = C:WindowsSysWOW64
ProgramFilesX86 folder = C:Program Files (x86)
CommonProgramFiles folder = C:Program Files (x86)Common Files
CommonProgramFilesX86 folder = C:Program Files (x86)Common Files
CommonTemplates folder = C:ProgramDataMicrosoftWindowsTemplates
CommonDocuments folder = C:UsersPublicDocuments
CommonAdminTools folder = C:ProgramDataMicrosoftWindowsStart MenuProgramsAdministrative Tools
AdminTools folder = C:UsersCYMAppDataRoamingMicrosoftWindowsStart MenuProgramsAdministrative Tools
CommonMusic folder = C:UsersPublicMusic
CommonPictures folder = C:UsersPublicPictures
CommonVideos folder = C:UsersPublicVideos
Resources folder = C:Windowsresources
LocalizedResources folder =
CommonOemLinks folder =
CDBurning folder = C:UsersCYMAppDataLocalMicrosoftWindowsBurnBurn
二、使用Enviroment.GetEnvironmentVariable
可以看到这里使用了获取环境变量的功能,所以可以通过这个方式获取环境变量的值,这里也就是指环境变量中存储的文件夹地址。
这里举例说明如何使用这种方式:
string dir = Enviroment.GetEnviromentVariable("Java_Home");
这行代码执行之后,dir的值将变为环境变量“Java_Home”存储的值,即Java的安装路径。
下面的代码将枚举本地系统所有可以获取的值:
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
Console.WriteLine(" {0} value = {1}", de.Key, de.Value);
}
Console.WriteLine(" {0} value = {1}", de.Key, de.Value);
}
在本地系统(64位 Windows10)上运行结果如下:
PROCESSOR_ARCHITEW6432 value = AMD64
COMPUTERNAME value = DESKTOP-MKB01RR
CommonProgramFiles(x86) value = C:Program Files (x86)Common Files
HOMEPATH value = UsersCYM
VisualStudioEdition value = Microsoft Visual Studio Community 2015
TMP value = C:UsersCYMAppDataLocalTemp
PROCESSOR_REVISION value = 3a09
PATHEXT value = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PkgDefApplicationConfigFile value = C:UsersCYMAppDataLocalMicrosoftVisualStudio14.0devenv.exe.config
USERDOMAIN_ROAMINGPROFILE value = DESKTOP-MKB01RR
TEMP value = C:UsersCYMAppDataLocalTemp
LOCALAPPDATA value = C:UsersCYMAppDataLocal
PUBLIC value = C:UsersPublic USERDOMAIN value = DESKTOP-MKB01RR
ProgramFiles(x86) value = C:Program Files (x86)
Path value = C:ProgramDataOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)NVIDIA CorporationPhysXCommon;D:Program FilesJavajre1.8.0_51bin;D:Program FilesJavajre1.8.0_51jrebin;C:Windowssystem32configsystemprofile.dnxbin;C:Program FilesMicrosoft DNXDnvm;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program Files (x86)MySQLMySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5;C:Program Files (x86)MySQLMySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5Doctrine extensions for PHP
PROCESSOR_LEVEL value = 6
PROCESSOR_IDENTIFIER value = Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
PSModulePath value = C:Windowssystem32WindowsPowerShellv1.0Modules
NUMBER_OF_PROCESSORS value = 8
FPS_BROWSER_USER_PROFILE_STRING value = Default
CommonProgramFiles value = C:Program Files (x86)Common Files
ClassPath value = .;D:Program FilesJavajre1.8.0_51bin;D:Program FilesJavajre1.8.0_51libdt.jar;D:Program FilesJavajre1.8.0_51libtools.jar
VisualStudioDir value = C:UsersCYMDocumentsVisual Studio 2015
ProgramData value = C:ProgramData
ProgramW6432 value = C:Program Files
ProgramFiles value = C:Program Files (x86)
ENABLE_XAML_DIAGNOSTICS_SOURCE_INFO value = 1
Java_Home value = D:Program FilesJavajre1.8.0_51
SystemRoot value = C:Windows
SESSIONNAME value = Console
VisualStudioVersion value = 14.0
CommonProgramW6432 value = C:Program FilesCommon Files
LOGONSERVER value = \MicrosoftAccount
USERPROFILE value = C:UsersCYM
MSBuildLoadMicrosoftTargetsReadOnly value = true
VS140COMNTOOLS value = D:Program Files (x86)Microsoft Visual Studio 14.0Common7Tools
VSLANG value = 2052
APPDATA value = C:UsersCYMAppDataRoaming HOMEDRIVE value = C:
USERNAME value = CYM
FPS_BROWSER_APP_PROFILE_STRING value = Internet Explorer
PROCESSOR_ARCHITECTURE value = x86
OS value = Windows_NT
ComSpec value = C:Windowssystem32cmd.exe
SystemDrive value = C:
windir value = C:Windows
ALLUSERSPROFILE value = C:ProgramData
COMPUTERNAME value = DESKTOP-MKB01RR
CommonProgramFiles(x86) value = C:Program Files (x86)Common Files
HOMEPATH value = UsersCYM
VisualStudioEdition value = Microsoft Visual Studio Community 2015
TMP value = C:UsersCYMAppDataLocalTemp
PROCESSOR_REVISION value = 3a09
PATHEXT value = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PkgDefApplicationConfigFile value = C:UsersCYMAppDataLocalMicrosoftVisualStudio14.0devenv.exe.config
USERDOMAIN_ROAMINGPROFILE value = DESKTOP-MKB01RR
TEMP value = C:UsersCYMAppDataLocalTemp
LOCALAPPDATA value = C:UsersCYMAppDataLocal
PUBLIC value = C:UsersPublic USERDOMAIN value = DESKTOP-MKB01RR
ProgramFiles(x86) value = C:Program Files (x86)
Path value = C:ProgramDataOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)NVIDIA CorporationPhysXCommon;D:Program FilesJavajre1.8.0_51bin;D:Program FilesJavajre1.8.0_51jrebin;C:Windowssystem32configsystemprofile.dnxbin;C:Program FilesMicrosoft DNXDnvm;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program Files (x86)MySQLMySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5;C:Program Files (x86)MySQLMySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5Doctrine extensions for PHP
PROCESSOR_LEVEL value = 6
PROCESSOR_IDENTIFIER value = Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
PSModulePath value = C:Windowssystem32WindowsPowerShellv1.0Modules
NUMBER_OF_PROCESSORS value = 8
FPS_BROWSER_USER_PROFILE_STRING value = Default
CommonProgramFiles value = C:Program Files (x86)Common Files
ClassPath value = .;D:Program FilesJavajre1.8.0_51bin;D:Program FilesJavajre1.8.0_51libdt.jar;D:Program FilesJavajre1.8.0_51libtools.jar
VisualStudioDir value = C:UsersCYMDocumentsVisual Studio 2015
ProgramData value = C:ProgramData
ProgramW6432 value = C:Program Files
ProgramFiles value = C:Program Files (x86)
ENABLE_XAML_DIAGNOSTICS_SOURCE_INFO value = 1
Java_Home value = D:Program FilesJavajre1.8.0_51
SystemRoot value = C:Windows
SESSIONNAME value = Console
VisualStudioVersion value = 14.0
CommonProgramW6432 value = C:Program FilesCommon Files
LOGONSERVER value = \MicrosoftAccount
USERPROFILE value = C:UsersCYM
MSBuildLoadMicrosoftTargetsReadOnly value = true
VS140COMNTOOLS value = D:Program Files (x86)Microsoft Visual Studio 14.0Common7Tools
VSLANG value = 2052
APPDATA value = C:UsersCYMAppDataRoaming HOMEDRIVE value = C:
USERNAME value = CYM
FPS_BROWSER_APP_PROFILE_STRING value = Internet Explorer
PROCESSOR_ARCHITECTURE value = x86
OS value = Windows_NT
ComSpec value = C:Windowssystem32cmd.exe
SystemDrive value = C:
windir value = C:Windows
ALLUSERSPROFILE value = C:ProgramData
以上便是两种常用的系统特殊文件夹的地址获取方式。
最后
以上就是无私人生为你收集整理的[C#]Windows系统特殊文件夹路径获取的全部内容,希望文章能够帮你解决[C#]Windows系统特殊文件夹路径获取所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复