MONO中文MONO平台技术软件开发C#编程语言 C#返回系统特殊目录路径

1  /  1  页   1 跳转 查看:2146

C#返回系统特殊目录路径

C#返回系统特殊目录路径

我记得以前干过这事,记不起来了,网上一搜,看到好多网站都在转载一篇取c#特殊目录的文章

http://www.sinoprise.cn/showtopic-27.aspx

一个MS的MVP写的,我记得没这么麻烦,呵呵,不甘心,再找,终于有找到一篇;

http://badguy.d113.163ns.com/article.asp?id=569

呵呵,这就对了.

在System命名空间中有一个取设备环境配置的类: Environment


ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref2/html/T_System_Environment.htm


这个类有一个叫做: GetFolderPath的方法可以取得特殊文件夹的路径.


ApplicationData 目录
它用作当前漫游用户的应用程序特定数据的公共储存库。
漫游用户在网络上的多台计算机上工作。漫游用户的配置文件保存在网络服务器上,当用户登录到某个系统上时,它会加载到该系统。

CommonApplicationData 目录
它用作所有用户使用的应用程序特定数据的公共储存库。

CommonProgramFiles
用于应用程序间共享的组件的目录。
 
Cookies
用作 Internet Cookie 的公共储存库的目录。 

Desktop
逻辑桌面,而不是物理文件系统位置。

DesktopDirectory
用于物理上存储桌面上的文件对象的目录。不应将此目录与桌面文件夹本身混淆,后者是虚拟文件夹。

Favorites
用作用户收藏夹项的公共储存库的目录。 

History
用作 Internet 历史记录项的公共储存库的目录。 

InternetCache
用作 Internet 临时文件的公共储存库的目录。 

LocalApplicationData 目录
它用作当前非漫游用户使用的应用程序特定数据的公共储存库。

MyComputer
“我的电脑”文件夹。 
注意:由于没有为“我的电脑”文件夹定义路径,因此 MyComputer 常数将始终生成空字符串 ("")。

MyDocuments
“我的文档”文件夹。 

Personal
用作文档的公共储存库的目录。 

ProgramFiles
“Program files”目录。

Programs
包含用户程序组的目录。
 
Recent
包含用户最近使用过的文档的目录。 

SendTo
包含“发送”菜单项的目录。
 
StartMenu
包含“开始”菜单项的目录。 

Startup
对应于用户的“启动”程序组的目录。
每当用户登录、启动 Windows NT 或更高版本或启动 Windows 98 时,系统均会启动这些程序。

System
“System”目录。 

Templates
用作文档模板的公共储存库的目录。 



好像没有Fonts目录,没关系,因为我们知道Windows的目录结构,所以这样可以得到Fonts的目录:


Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).FullName+@"\Fonts";


备注

系统特殊文件夹是包含公共信息的文件夹,如“Program Files”、“Programs”、“System”或“Startup”。特殊文件夹在默认情况下由系统设置,或者由用户在安装 Windows 的某个版本时显式进行设置。
 

附:如何用VB获得Windows各类系统目录

如果是在取不到想要的目录,笔者也不建议用读取注册表的办法,除非除此之外,别无他途.随附一个用API取Windows特殊目录的方法,虽是VB写的,API比较简单,用VC,Delphi,和C#去做也不会有什么困难.

出处: http://v3.7880.com/Info/Article-511aa260.html

现在有很多关于如何用VB获得Windows目录的文章,但大都只讲到如何获得Windows目录和System目录,有时候我们却需要获得像"我的文档"这样的目录("我的文档"的路径并不是固定的,可以由自己设定,也有可能因为系统的安装路径不同而不同),那又该如何处理呢?下面我们来具体谈谈如何用VB获得这种路径。
  先向大家介绍两个API函数,这两个函数分别是SHGetSpecialFolderLocation和SHGetPathFromIDList,这就是我们用来获得各种路径的武器。
函数声明:
Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long

函数功能及参数说明
SHGetSpecialFolderLocation:获得某个特殊目录在特殊目录列表中的位置;它有三个参数,第一个参数是用来指定所有者窗口的,在应用中一般我们写上"0"就可以了;第二个参数是一个整数id,它决定要查找的目录是哪一个目录,它的取值可能如下:
&H0& '桌面
&H2& '程序集
&H5& '我的文档
&H6& '收藏夹
&H7& '启动
&H8& '最近打开的文件
&H9& '发送
&HB& '开始菜单
&H13& '网上邻居
&H14& '字体
&H15& 'ShellNew
&H1A& 'Application Data
&H1B& 'PrintHood
&H20& '网页临时文件
&H21& 'Cookies目录
&H22& '历史
第三个参数是获得的特殊目录在特殊目录列表中的地址。
SHGetPathFromIDList:根据某特殊目录在特殊目录列表中的地址获取该目录的准确路径。它有两个参数,第一个参数是特殊目录在特殊目录列表中的地址,也即上一个函数所获得的地址;第二个参数是一个字符串型数据,用来保存返回的特殊目录的准确路径。
比如:为了获得DeskTop的路径,首先需调用SHGetSpecialFolderLocation获得DeskTop在特殊目录列表中的位置Pid,然后调用SHGetPathFromIDList函数获得Pid指向的列表内容,即DeskTop的准确路径。
下面是我编写的一个用来获取Windows各种目录路径的例子,供大家参考。如果您有什么问题或建议,欢迎给我来信(xuhaoliang@21cn.com)。
程序界面如下:


程序代码如下:
Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Const MAX_LEN = 200
'字符串最大长度
Const DESKTOP = &H0& '桌面
Const PROGRAMS = &H2& '程序集
Const MYDOCUMENTS = &H5& '我的文档
Const MYFAVORITES = &H6& '收藏夹
Const STARTUP = &H7& '启动
Const RECENT = &H8& '最近打开的文件
Const SENDTO = &H9& '发送
Const STARTMENU = &HB& '开始菜单
Const NETHOOD = &H13& '网上邻居
Const FONTS = &H14& '字体
Const SHELLNEW = &H15& 'ShellNew
Const APPDATA = &H1A& 'Application Data
Const PRINTHOOD = &H1B& 'PrintHood
Const PAGETMP = &H20& '网页临时文件
Const COOKIES = &H21& 'Cookies目录
Const HISTORY = &H22& '历史
Private Sub Command2_Click()
End
End Sub

Private Sub Form_Load()
Dim sTmp As String * MAX_LEN
  '存放结果的固定长度的字符串
Dim nLength As Long '字符串的实际长度
Dim pidl As Long  '某特殊目录在特殊目录列表中的位置
'*************************获得Windows目录**********************************
Length = GetWindowsDirectory(sTmp, MAX_LEN)
txtWin.Text = Left(sTmp, Length)

'*************************获得System目录***********************************
Length = GetSystemDirectory(sTmp, MAX_LEN)
txtSystem.Text = Left(sTmp, Length)

'*************************获得Temp目录***********************************
Length = GetTempPath(MAX_LEN, sTmp)
txtTemp.Text = Left(sTmp, Length)

'*************************获得DeskTop目录**********************************
SHGetSpecialFolderLocation 0, DESKTOP, pidl
SHGetPathFromIDList pidl, sTmp
txtDesktop.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
'*************************获得发送到目录**********************************
SHGetSpecialFolderLocation 0, SENDTO, pidl
SHGetPathFromIDList pidl, sTmp
txtSendTo.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得我的文档目录*********************************
SHGetSpecialFolderLocation 0, MYDOCUMENTS, pidl
SHGetPathFromIDList pidl, sTmp
txtDocument.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得程序集目录***********************************
SHGetSpecialFolderLocation 0, PROGRAMS, pidl
SHGetPathFromIDList pidl, sTmp
txtProgram.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得启动目录*************************************
SHGetSpecialFolderLocation 0, STARTUP, pidl
SHGetPathFromIDList pidl, sTmp
txtStart.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得开始菜单目录*********************************
SHGetSpecialFolderLocation 0, STARTMENU, pidl
SHGetPathFromIDList pidl, sTmp
txtStartMenu.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得收藏夹目录***********************************
SHGetSpecialFolderLocation 0, MYFAVORITES, pidl
SHGetPathFromIDList pidl, sTmp
txtFavorites.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'**********************获得最后打开的文件目录*******************************
SHGetSpecialFolderLocation 0, RECENT, pidl
SHGetPathFromIDList pidl, sTmp
txtRecent.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得网上邻居目录*********************************
SHGetSpecialFolderLocation 0, NETHOOD, pidl
SHGetPathFromIDList pidl, sTmp
txtNetHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得字体目录**********************************
SHGetSpecialFolderLocation 0, FONTS, pidl
SHGetPathFromIDList pidl, sTmp
txtFonts.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得Cookies目录**********************************
SHGetSpecialFolderLocation 0, COOKIES, pidl
SHGetPathFromIDList pidl, sTmp
txtCookies.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得历史目录**********************************
SHGetSpecialFolderLocation 0, HISTORY, pidl
SHGetPathFromIDList pidl, sTmp
txtHistory.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'***********************获得网页临时文件目录*******************************
SHGetSpecialFolderLocation 0, PAGETMP, pidl
SHGetPathFromIDList pidl, sTmp
txtPageTmp.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得ShellNew目录*********************************
SHGetSpecialFolderLocation 0, SHELLNEW, pidl
SHGetPathFromIDList pidl, sTmp
txtShellNew.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'***********************获得Application Data目录*****************************
SHGetSpecialFolderLocation 0, APPDATA, pidl
SHGetPathFromIDList pidl, sTmp
txtAppData.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)

'*************************获得PrintHood目录*********************************
SHGetSpecialFolderLocation 0, PRINTHOOD, pidl
SHGetPathFromIDList pidl, sTmp
txtPrintHood.Text = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
End Sub
 
1  /  1  页   1 跳转

版权所有 Sinoprise Network Studio   Sitemap

Powered by Discuz!NT 2.0.1214    Copyright © 2001-2009 Comsenz Inc.
Processed in 0.109375 second(s) , 7 queries. 京ICP备05062328号
返顶部