通过Environment.SpecialFolder枚举参数获取系统特定的目录

System.Environment.GetFolderPath函数可以通过Environment.SpecialFolder枚举参数获取系统特定的目录,比如桌面,我的文档,收藏夹等..

但是有部分参数.NetFrameWork2.0并未开放,比如AllUser用户的这些特殊目录。我们可以通过windowsApi函数SHGetFolderPath来获取,如果你能通过Reflector看一下FrameWork代码就明白了。
  代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
...{
  class Program
  ...{
    [DllImport("shfolder.dll", CharSet = CharSet.Auto)]
    internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
  
    static void Main(string[] args)
    ...{
      for (int i = 0; i <= 255; i++)
      ...{
        StringBuilder lpszPath = new StringBuilder(260);
  
        SHGetFolderPath(IntPtr.Zero, i, IntPtr.Zero, 0, lpszPath);
  
        Console.WriteLine(i.ToString() + ":" + lpszPath);
      }
      Console.Read();
    }
  }
}

 

结果如下


0:C:Documents and SettingsAdministrator桌面
1:
2:C:Documents and SettingsAdministrator「开始」菜单程序
3:
4:
5:C:Documents and SettingsAdministratorMy Documents
6:C:Documents and SettingsAdministratorFavorites
7:C:Documents and SettingsAdministrator「开始」菜单程序启动
8:C:Documents and SettingsAdministratorRecent
9:C:Documents and SettingsAdministratorSendTo
10:
11:C:Documents and SettingsAdministrator「开始」菜单
12:
13:C:Documents and SettingsAdministratorMy DocumentsMy Music
14:C:Documents and SettingsAdministratorMy DocumentsMy Videos
15:
16:C:Documents and SettingsAdministrator桌面
17:
18:
19:C:Documents and SettingsAdministratorNetHood
20:C:WINDOWSFonts
21:C:Documents and SettingsAdministratorTemplates
22:C:Documents and SettingsAll Users「开始」菜单
23:C:Documents and SettingsAll Users「开始」菜单程序
24:C:Documents and SettingsAll Users「开始」菜单程序启动
25:C:Documents and SettingsAll Users桌面
26:C:Documents and SettingsAdministratorApplication Data
27:C:Documents and SettingsAdministratorPrintHood
28:C:Documents and SettingsAdministratorLocal SettingsApplication Data
29:
30:
31:C:Documents and SettingsAll UsersFavorites
32:C:Documents and SettingsAdministratorLocal SettingsTemporary Internet Files
33:C:Documents and SettingsAdministratorCookies
34:C:Documents and SettingsAdministratorLocal SettingsHistory
35:C:Documents and SettingsAll UsersApplication Data
36:C:WINDOWS
37:C:WINDOWSsystem32
38:C:Program Files
39:C:Documents and SettingsAdministratorMy DocumentsMy Pictures
40:C:Documents and SettingsAdministrator
41:C:WINDOWSsystem32
42:
43:C:Program FilesCommon Files
44:
45:C:Documents and SettingsAll UsersTemplates
46:C:Documents and SettingsAll UsersDocuments
47:C:Documents and SettingsAll Users「开始」菜单程序管理工具
48:C:Documents and SettingsAdministrator「开始」菜单程序管理工具
49:
50:
51:
52:
53:C:Documents and SettingsAll UsersDocumentsMy Music
54:
55:C:Documents and SettingsAll UsersDocumentsMy Videos
56:C:WINDOWSresources
57:
58:
59:C:Documents and SettingsAdministratorLocal SettingsApplication DataMicrosoftCD Burning


  具体意义可以参照Environment.SpecialFolder枚举说明和windows基本常识来理解。

 

你可能感兴趣的:(通过Environment.SpecialFolder枚举参数获取系统特定的目录)