C# 获取windows特殊路径
虽然是古老的问题,最近用到这个,查一下还不少东东呐
一、使用Environment.SpecialFolder
该方法最简单,直接使用即可,只是提供的特殊路径比较少。
(1)使用方法:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
(2)获取参数:
foreach (Environment.SpecialFolder s in
Enum.GetValues(typeof(Environment.SpecialFolder)))
{
Console.WriteLine("{0} folder : {1}",
s, Environment.GetFolderPath(s));
}
(3)输出参数列表:
Desktop = C:\Documents and Settings\gmj\桌面
Programs = C:\Documents and Settings\gmj\「开始」菜单\程序
Personal = E:\我的文档
Personal = E:\我的文档
Favorites = C:\Documents and Settings\gmj\Favorites
Startup = C:\Documents and Settings\gmj\「开始」菜单\程序\启动
Recent = C:\Documents and Settings\gmj\Recent
SendTo = C:\Documents and Settings\gmj\SendTo
StartMenu = C:\Documents and Settings\gmj\「开始」菜单
MyMusic = E:\我的文档\My Music
DesktopDirectory = C:\Documents and Settings\gmj\桌面
MyComputer =
Templates = C:\Documents and Settings\gmj\Templates
ApplicationData = C:\Documents and Settings\gmj\Application Data
LocalApplicationData = C:\Documents and Settings\gmj\Local Settings\Application Data
InternetCache = C:\Documents and Settings\gmj\Local Settings\Temporary Internet Files
Cookies = C:\Documents and Settings\gmj\Cookies
History = C:\Documents and Settings\gmj\Local Settings\History
CommonApplicationData = C:\Documents and Settings\All Users\Application Data
System = C:\WINDOWS\system32
ProgramFiles = C:\Program Files
MyPictures = E:\我的文档\My Pictures
CommonProgramFiles = C:\Program Files\Common Files
合计:23行
二、使用Environment.GetEnvironmentVariable
该方法可提供更多内容。
(1)使用方法:
string path = Environment.GetEnvironmentVariable("ALLUSERSPROFILE");
(2)获取参数:
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
Console.WriteLine(" {0} = {1}", de.Key, de.Value);
}
(3)输出参数列表:
Path = D:\Program Files\Sun\OPhone SDK\tools;D:\Program Files\Sun\android-sdk-windows-1.5_r2\tools;D:\Program
Files\Sun\SDK\jdk\bin;D:\Program Files\Sun\SDK\jdk\jre\bin;E:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;C:\Program
Files\Oracle\jre\1.1.8\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Intel\DMIX;C:\Program
Files\Microsoft SQL Server\90\Tools\binn\;D:\Program Files\CodeSmith\v5.0\;D:\Program Files\Symantec\pcAnywhere\;C:\Program
Files\Ixia\IxChariot;D:\Program Files\Sun\SDK\bin;
TEMP = C:\DOCUME~1\gmj\LOCALS~1\Temp
SESSIONNAME = Console
PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
USERDOMAIN = EVER62
PROCESSOR_ARCHITECTURE = x86
SystemDrive = C:
APPDATA = C:\Documents and Settings\gmj\Application Data
JSERV = E:\oracle\ora92/Apache/Jserv/conf
windir = C:\WINDOWS
_ACP_INCLUDE = D:\Program Files\Microsoft Visual Studio 8\VC\include;D:\Program Files\Microsoft Visual Studio 8
\VC\atlmfc\include;D:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include;D:\Program Files\Microsoft Visual Studio 8
\SDK\v2.0\include
TMP = C:\DOCUME~1\gmj\LOCALS~1\Temp
USERPROFILE = C:\Documents and Settings\gmj
ProgramFiles = C:\Program Files
FP_NO_HOST_CHECK = NO
HOMEPATH = \Documents and Settings\gmj
COMPUTERNAME = EVER62
_ACP_PATH = D:\Program Files\Microsoft Visual Studio 8\VC\bin;D:\Program Files\Microsoft Visual Studio 8
\VC\PlatformSDK\bin;D:\Program Files\Microsoft Visual Studio 8\Common7\Tools\bin;D:\Program Files\Microsoft Visual Studio 8
\Common7\tools;D:\Program Files\Microsoft Visual Studio 8\Common7\ide;C:\Program Files\HTML Help Workshop;D:\Program
Files\Microsoft Visual Studio 8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;D:\Program Files\Microsoft Visual
Studio 8\;D:\Program Files\Microsoft Visual Studio 8\\SDK\v2.0\bin;C:\WINDOWS\SysWow64;D:\Program Files\Microsoft Visual Studio
8\Team Tools\Static Analysis Tools\FxCop\;D:\Program Files\Sun\OPhone SDK\tools;D:\Program Files\Sun\android-sdk-windows-1.5_r2
\tools;D:\Program Files\Sun\SDK\jdk\bin;D:\Program Files\Sun\SDK\jdk\jre\bin;E:\oracle\ora92\bin;C:\Program
Files\Oracle\jre\1.3.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32
\Wbem;C:\Program Files\Intel\DMIX;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;D:\Program Files\CodeSmith\v5.0
\;D:\Program Files\Symantec\pcAnywhere\;C:\Program Files\Ixia\IxChariot;D:\Program Files\Sun\SDK\bin;
USERNAME = gmj
NUMBER_OF_PROCESSORS = 2
PROCESSOR_IDENTIFIER = x86 Family 6 Model 23 Stepping 6, GenuineIntel
_ACP_LIB = D:\Program Files\Microsoft Visual Studio 8\VC\lib;D:\Program Files\Microsoft Visual Studio 8
\VC\atlmfc\lib;D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\lib\i386;D:\Program Files\Microsoft Visual Studio 8
\VC\PlatformSDK\lib;D:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\lib;D:\Program Files\Microsoft Visual Studio 8
\;D:\Program Files\Microsoft Visual Studio 8\lib
WecVersionForRosebud.DA8 = 2
SystemRoot = C:\WINDOWS
ComSpec = C:\WINDOWS\system32\cmd.exe
LOGONSERVER = http://www.cnblogs.com/xinyuxin912/***
VS80COMNTOOLS = D:\Program Files\Microsoft Visual Studio 8\Common7\Tools\
_ACP_ATLPROV = D:\Program Files\Microsoft Visual Studio 8\VC\Bin\ATLProv.dll
WV_GATEWAY_CFG = E:\oracle\ora92\Apache\modplsql\cfg\wdbsvr.app
CommonProgramFiles = C:\Program Files\Common Files
PROCESSOR_LEVEL = 6
PROCESSOR_REVISION = 1706
CLIENTNAME = Console
ALLUSERSPROFILE = C:\Documents and Settings\All Users
VS90COMNTOOLS = D:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\
OS = Windows_NT
HOMEDRIVE = C:
合计:37行
三、调用shell32中的SHGetSpecialFolderPath
需要引用"System.Runtime.InteropServices”命名空间。
(1)使用方法:
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,[Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_PROGRAMS = 2; // \Windows\Start Menu\Programs
StringBuilder pathBuilder = new StringBuilder(260);
SHGetSpecialFolderPath(IntPtr.Zero, pathBuilder, CSIDL_PROGRAMS, false);
string path = pathBuilder.ToString(); // should now hold the path
(2)参数列表:
Here's the full list of CSIDL's from ShlObj.h
constint CSIDL_DESKTOP =0x0000; // <desktop>
constint CSIDL_INTERNET =0x0001; // Internet Explorer (icon on desktop)
constint CSIDL_PROGRAMS =0x0002; // Start Menu\Programs
constint CSIDL_CONTROLS =0x0003; // My Computer\Control Panel
constint CSIDL_PRINTERS =0x0004; // My Computer\Printers
constint CSIDL_PERSONAL =0x0005; // My Documents
constint CSIDL_FAVORITES =0x0006; // <user name>\Favorites
constint CSIDL_STARTUP =0x0007; // Start Menu\Programs\Startup
constint CSIDL_RECENT =0x0008; // <user name>\Recent
constint CSIDL_SENDTO =0x0009; // <user name>\SendTo
constint CSIDL_BITBUCKET =0x000a; // <desktop>\Recycle Bin
constint CSIDL_STARTMENU =0x000b; // <user name>\Start Menu
constint CSIDL_MYDOCUMENTS = CSIDL_PERSONAL;// Personal was just a silly name for My Documents
constint CSIDL_MYMUSIC =0x000d; // "My Music" folder
constint CSIDL_MYVIDEO =0x000e; // "My Videos" folder
constint CSIDL_DESKTOPDIRECTORY =0x0010; // <user name>\Desktop
constint CSIDL_DRIVES =0x0011; // My Computer
constint CSIDL_NETWORK =0x0012; // Network Neighborhood (My Network Places)
constint CSIDL_NETHOOD =0x0013; // <user name>\nethood
constint CSIDL_FONTS =0x0014; // windows\fonts
constint CSIDL_TEMPLATES =0x0015;
constint CSIDL_COMMON_STARTMENU =0x0016; // All Users\Start Menu
constint CSIDL_COMMON_PROGRAMS =0x0017; // All Users\Start Menu\Programs
constint CSIDL_COMMON_STARTUP =0x0018; // All Users\Startup
constint CSIDL_COMMON_DESKTOPDIRECTORY =0x0019; // All Users\Desktop
constint CSIDL_APPDATA =0x001a; // <user name>\Application Data
constint CSIDL_PRINTHOOD =0x001b; // <user name>\PrintHood
constint CSIDL_LOCAL_APPDATA =0x001c; // <user name>\Local Settings\Applicaiton Data (non roaming)
constint CSIDL_ALTSTARTUP =0x001d; // non localized startup
constint CSIDL_COMMON_ALTSTARTUP =0x001e; // non localized common startup
constint CSIDL_COMMON_FAVORITES =0x001f;
constint CSIDL_INTERNET_CACHE =0x0020;
constint CSIDL_COOKIES =0x0021;
constint CSIDL_HISTORY =0x0022;
constint CSIDL_COMMON_APPDATA =0x0023; // All Users\Application Data
constint CSIDL_WINDOWS =0x0024; // GetWindowsDirectory()
constint CSIDL_SYSTEM =0x0025; // GetSystemDirectory()
constint CSIDL_PROGRAM_FILES =0x0026; // C:\Program Files
constint CSIDL_MYPICTURES =0x0027; // C:\Program Files\My Pictures
constint CSIDL_PROFILE =0x0028; // USERPROFILE
constint CSIDL_SYSTEMX86 =0x0029; // x86 system directory on RISC
constint CSIDL_PROGRAM_FILESX86 =0x002a; // x86 C:\Program Files on RISC
constint CSIDL_PROGRAM_FILES_COMMON =0x002b; // C:\Program Files\Common
constint CSIDL_PROGRAM_FILES_COMMONX86 =0x002c; // x86 Program Files\Common on RISC
constint CSIDL_COMMON_TEMPLATES =0x002d; // All Users\Templates
constint CSIDL_COMMON_DOCUMENTS =0x002e; // All Users\Documents
constint CSIDL_COMMON_ADMINTOOLS =0x002f; // All Users\Start Menu\Programs\Administrative Tools
constint CSIDL_ADMINTOOLS =0x0030; // <user name>\Start Menu\Programs\Administrative Tools
constint CSIDL_CONNECTIONS =0x0031; // Network and Dial-up Connections
constint CSIDL_COMMON_MUSIC =0x0035; // All Users\My Music
constint CSIDL_COMMON_PICTURES =0x0036; // All Users\My Pictures
constint CSIDL_COMMON_VIDEO =0x0037; // All Users\My Video
constint CSIDL_RESOURCES =0x0038; // Resource Direcotry
constint CSIDL_RESOURCES_LOCALIZED =0x0039; // Localized Resource Direcotry
constint CSIDL_COMMON_OEM_LINKS =0x003a; // Links to All Users OEM specific apps
constint CSIDL_CDBURN_AREA =0x003b; // USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
constint CSIDL_COMPUTERSNEARME =0x003d; // Computers Near Me (computered from Workgroup membership)
constint CSIDL_FLAG_CREATE =0x8000; // combine with CSIDL_ value to force folder creation in SHGetFolderPath()
constint CSIDL_FLAG_DONT_VERIFY =0x4000; // combine with CSIDL_ value to return an unverified folder path
constint CSIDL_FLAG_DONT_UNEXPAND =0x2000; // combine with CSIDL_ value to avoid unexpanding environment variables
constint CSIDL_FLAG_NO_ALIAS =0x1000; // combine with CSIDL_ value to insure non-alias versions of the pidl
constint CSIDL_FLAG_PER_USER_INIT =0x0800; // combine with CSIDL_ value to indicate per-user init (eg. upgrade)
四、期待.net4.0
还没装.net4.0呢,好像已经扩展了SpecialFolder,比如我要获取 "C:\Documents and Settings\All Users\「开始」菜单\程序",则可以直接使用
string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms);
没实际验证过,如果您验证了帮我补充一
Example:
// C:\Users\USERNAME\Documents
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
C# 获取windows特殊路径的更多相关文章
- 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别
原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...
- Windows获取进程完整路径
#include <stdio.h> #include <locale.h> #include <windows.h> #include <tlhelp32. ...
- .NET中获取Windows的常见路径
更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月30日. 一.说明 每种平台都其预定义的规则,比如Windows平台有自己的规定.公司也有预定义的规章制度.那么在平台上开发和干活 ...
- C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖)
C#.ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 ============================================ 使用 Application.Start ...
- C#: 获取执行程序所在路径和启动资源管理器
一. 获取执行程序所在路径 1.获取和设置当前目录的完全限定路径. string str = System.Environment.CurrentDirectory; //获取的是主程序目录,线程启 ...
- 使用GetLogicalDriveStrings获取驱动器根路径
使用GetLogicalDriveStrings获取驱动器根路径,并使用自定义的GetDriveInfo函数获取驱动器的属性. VS2012 + win7 x64下调试通过. #include < ...
- 几个获取Windows系统信息的Delphi程序
1.获取windows版本信息 可以通过Windows API函数GetVersionEx来获得. 具体程序如下: Procedure Tform1.Button1Click(sender:TObje ...
- 获取Windows系统中的所有可用和在用串口
目的:获取Windows系统中的所有可用和在用串口 方法:注册表查询法 优点:简单.实用.快速.无遗漏,无多余结果. 说明:另外还有8种方法可以枚举串口,但都不如此法. 代码和详细注释如下: //-- ...
- 获取Windows服务下当前路径的方法
获取Windows服务下当前路径的方法 获取当前运行程序路径 包含exe Assembly.GetExecutingAssembly().Location; D:\xxxxxx\bin\Debug\x ...
随机推荐
- core java 1~4(HelloWorld & 标识符|关键字|数据类型 & 表达式|流程控制 & 数组)
MODULE 1 Java的编译和运行----------------------------编译:javac -d bin src\HelloWorld.java -d:指定编译后的class 文件 ...
- umask设置导致的weblogic中的应用上传的文件没有权限打开
去年,在公司的某一weblogic上部署的web应用上传文件后却没有读的权限.因为weblogic在Linux上部署,上传文件是mount到了一台安装了NFS的Windows Server上. 当时本 ...
- angularjs+nodejs+mongodb三件套
说实话,自己对于web前段的认识并不是太深入,但是因为项目的需要,所以有的时候肯定会需要接触到web前段的知识点.说到web前端想必大家肯定会想到css+js+html,的确web前端的工作,从某总角 ...
- vnext 技术两篇文章和评论
研究vnext的两篇 好文章,重点看评论! http://www.cnblogs.com/shanyou/p/4589930.html http://www.cnblogs.com/shanyou/p ...
- b75,gtx560,I5 安装10.10.2
1.安装变色龙,wowpc.iso,这个是可以让电脑从windows引导 mac 安装的. 2.把黑苹果CDR压到一个硬盘分区里去. 3.安装10.10.2,把安装盘里的extra拷贝到 系统盘里 , ...
- JS跨域方法及原理
JS跨域分析判断 JS跨域:在不同域之间,JS进行数据传输或通信.比如ajax向不同的域请求数据.JS获取iframe中的页面中的值(iframe内外不同域) 只要协议.端口.域名有一个不同则 ...
- ExtJS MVC结构
概述 大型的应用在开发和运维上都存在着困难.应用功能的调整和开发人员的调动都会影响对项目的掌控.ExtJS4带来了一种新的应用结构.这种结构不止用于组织代码,也能有效的减少必要的代码量. 这次ExtJ ...
- Java缓冲流细节
FileOutPutStream继承OutputStream,并不提供flush()方法的重写所以无论内容多少write都会将二进制流直接传递给底层操作系统的I/O,flush无效果.而Buffere ...
- javascript学习小记(一)
大四了,课少了许多,突然之间就不知道学什么啦.整天在宿舍混着日子,很想学习就是感觉没有一点头绪,昨天看了电影激战.这种纠结的情绪让我都有点喘不上气啦!一点要找点事情干了,所以决定找个东西开始学习.那就 ...
- GCC常用命令行一览表
GCC常用命令行一览表 这些常用的 gcc/g++ 命令行参数,你都知道么?1. gcc -E source_file.c-E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_ ...