[C#]Windows系统特殊文件夹路径获取

原文:[C#]Windows系统特殊文件夹路径获取

由于软件开发的需要,近期对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));

}

在个人计算机中(64位 Windows10系统)运行结果如下:

Desktop folder = C:\Users\CYM\Desktop

Programs folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

MyDocuments folder = C:\Users\CYM\Documents

MyDocuments folder = C:\Users\CYM\Documents

Favorites folder = C:\Users\CYM\Favorites

Startup folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Recent folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Recent

SendTo folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\SendTo

StartMenu folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Start Menu

MyMusic folder = C:\Users\CYM\Music

MyVideos folder = C:\Users\CYM\Videos

DesktopDirectory folder = C:\Users\CYM\Desktop

MyComputer folder =

NetworkShortcuts folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Network Shortcuts

Fonts folder = C:\Windows\Fonts

Templates folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Templates

CommonStartMenu folder = C:\ProgramData\Microsoft\Windows\Start Menu

CommonPrograms folder = C:\ProgramData\Microsoft\Windows\Start Menu\Programs

CommonStartup folder = C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

CommonDesktopDirectory folder = C:\Users\Public\Desktop

ApplicationData folder = C:\Users\CYM\AppData\Roaming

PrinterShortcuts folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Printer Shortcuts

LocalApplicationData folder = C:\Users\CYM\AppData\Local

InternetCache folder = C:\Users\CYM\AppData\Local\Microsoft\Windows\INetCache

Cookies folder = C:\Users\CYM\AppData\Local\Microsoft\Windows\INetCookies

History folder = C:\Users\CYM\AppData\Local\Microsoft\Windows\History

CommonApplicationData folder = C:\ProgramData

Windows folder = C:\Windows

System folder = C:\Windows\system32

ProgramFiles folder = C:\Program Files (x86)

MyPictures folder = C:\Users\CYM\Pictures

UserProfile folder = C:\Users\CYM

SystemX86 folder = C:\Windows\SysWOW64

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:\ProgramData\Microsoft\Windows\Templates

CommonDocuments folder = C:\Users\Public\Documents

CommonAdminTools folder = C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools

AdminTools folder = C:\Users\CYM\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools

CommonMusic folder = C:\Users\Public\Music

CommonPictures folder = C:\Users\Public\Pictures

CommonVideos folder = C:\Users\Public\Videos

Resources folder = C:\Windows\resources

LocalizedResources folder =

CommonOemLinks folder =

CDBurning folder = C:\Users\CYM\AppData\Local\Microsoft\Windows\Burn\Burn

二、使用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);

}

在本地系统(64位 Windows10)上运行结果如下:

PROCESSOR_ARCHITEW6432 value = AMD64

COMPUTERNAME value = DESKTOP-MKB01RR

CommonProgramFiles(x86) value = C:\Program Files (x86)\Common Files

HOMEPATH value = \Users\CYM

VisualStudioEdition value = Microsoft Visual Studio Community 2015

TMP value = C:\Users\CYM\AppData\Local\Temp

PROCESSOR_REVISION value = 3a09

PATHEXT value = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

PkgDefApplicationConfigFile value = C:\Users\CYM\AppData\Local\Microsoft\VisualStudio\14.0\devenv.exe.config

USERDOMAIN_ROAMINGPROFILE value = DESKTOP-MKB01RR

TEMP value = C:\Users\CYM\AppData\Local\Temp

LOCALAPPDATA value = C:\Users\CYM\AppData\Local

PUBLIC value = C:\Users\Public USERDOMAIN value = DESKTOP-MKB01RR

ProgramFiles(x86) value = C:\Program Files (x86)

Path value = C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Program Files\Java\jre1.8.0_51\bin;D:\Program Files\Java\jre1.8.0_51\jre\bin;C:\Windows\system32\config\systemprofile\.dnx\bin;C:\Program
Files\Microsoft DNX\Dnvm\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5\Doctrine
extensions for PHP\

PROCESSOR_LEVEL value = 6

PROCESSOR_IDENTIFIER value = Intel64 Family 6 Model 58 Stepping 9, GenuineIntel

PSModulePath value = C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

NUMBER_OF_PROCESSORS value = 8

FPS_BROWSER_USER_PROFILE_STRING value = Default

CommonProgramFiles value = C:\Program Files (x86)\Common Files

ClassPath value = .;D:\Program Files\Java\jre1.8.0_51\bin;D:\Program Files\Java\jre1.8.0_51\lib\dt.jar;D:\Program Files\Java\jre1.8.0_51\lib\tools.jar

VisualStudioDir value = C:\Users\CYM\Documents\Visual 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 Files\Java\jre1.8.0_51

SystemRoot value = C:\Windows

SESSIONNAME value = Console

VisualStudioVersion value = 14.0

CommonProgramW6432 value = C:\Program Files\Common Files

LOGONSERVER value = \\MicrosoftAccount

USERPROFILE value = C:\Users\CYM

MSBuildLoadMicrosoftTargetsReadOnly value = true

VS140COMNTOOLS value = D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\

VSLANG value = 2052

APPDATA value = C:\Users\CYM\AppData\Roaming 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:\Windows\system32\cmd.exe

SystemDrive value = C:

windir value = C:\Windows

ALLUSERSPROFILE value = C:\ProgramData


以上便是两种常用的系统特殊文件夹的地址获取方式。
上一篇:Python之路-Python中文件和异常


下一篇:python中的目录递归的创建和目录及其下的文件递归的删除