• 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 Settingsgmj桌面
    Programs = C:Documents and Settingsgmj「开始」菜单程序
    Personal = E:我的文档
    Personal = E:我的文档
    Favorites = C:Documents and SettingsgmjFavorites
    Startup = C:Documents and Settingsgmj「开始」菜单程序启动
    Recent = C:Documents and SettingsgmjRecent
    SendTo = C:Documents and SettingsgmjSendTo
    StartMenu = C:Documents and Settingsgmj「开始」菜单
    MyMusic = E:我的文档My Music
    DesktopDirectory = C:Documents and Settingsgmj桌面
    MyComputer = 
    Templates = C:Documents and SettingsgmjTemplates
    ApplicationData = C:Documents and SettingsgmjApplication Data
    LocalApplicationData = C:Documents and SettingsgmjLocal SettingsApplication Data
    InternetCache = C:Documents and SettingsgmjLocal SettingsTemporary Internet Files
    Cookies = C:Documents and SettingsgmjCookies
    History = C:Documents and SettingsgmjLocal SettingsHistory
    CommonApplicationData = C:Documents and SettingsAll UsersApplication Data
    System = C:WINDOWSsystem32
    ProgramFiles = C:Program Files
    MyPictures = E:我的文档My Pictures
    CommonProgramFiles = C:Program FilesCommon 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 FilesSunOPhone SDK ools;D:Program FilesSunandroid-sdk-windows-1.5_r2 ools;D:Program

    FilesSunSDKjdkin;D:Program FilesSunSDKjdkjrein;E:oracleora92in;C:Program FilesOraclejre1.3.1in;C:Program

    FilesOraclejre1.1.8in;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:Program FilesIntelDMIX;C:Program

    FilesMicrosoft SQL Server90Toolsinn;D:Program FilesCodeSmithv5.0;D:Program FilesSymantecpcAnywhere;C:Program

    FilesIxiaIxChariot;D:Program FilesSunSDKin;
      TEMP = C:DOCUME~1gmjLOCALS~1Temp
      SESSIONNAME = Console
      PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
      USERDOMAIN = EVER62
      PROCESSOR_ARCHITECTURE = x86
      SystemDrive = C:
      APPDATA = C:Documents and SettingsgmjApplication Data
      JSERV = E:oracleora92/Apache/Jserv/conf
      windir = C:WINDOWS
      _ACP_INCLUDE = D:Program FilesMicrosoft Visual Studio 8VCinclude;D:Program FilesMicrosoft Visual Studio 8

    VCatlmfcinclude;D:Program FilesMicrosoft Visual Studio 8VCPlatformSDKinclude;D:Program FilesMicrosoft Visual Studio 8

    SDKv2.0include
      TMP = C:DOCUME~1gmjLOCALS~1Temp
      USERPROFILE = C:Documents and Settingsgmj
      ProgramFiles = C:Program Files
      FP_NO_HOST_CHECK = NO
      HOMEPATH = Documents and Settingsgmj
      COMPUTERNAME = EVER62
      _ACP_PATH = D:Program FilesMicrosoft Visual Studio 8VCin;D:Program FilesMicrosoft Visual Studio 8

    VCPlatformSDKin;D:Program FilesMicrosoft Visual Studio 8Common7Toolsin;D:Program FilesMicrosoft Visual Studio 8

    Common7 ools;D:Program FilesMicrosoft Visual Studio 8Common7ide;C:Program FilesHTML Help Workshop;D:Program

    FilesMicrosoft Visual Studio 8SDKv2.0in;C:WINDOWSMicrosoft.NETFrameworkv2.0.50727;D:Program FilesMicrosoft Visual

    Studio 8;D:Program FilesMicrosoft Visual Studio 8\SDKv2.0in;C:WINDOWSSysWow64;D:Program FilesMicrosoft Visual Studio

    8Team ToolsStatic Analysis ToolsFxCop;D:Program FilesSunOPhone SDK ools;D:Program FilesSunandroid-sdk-windows-1.5_r2

    ools;D:Program FilesSunSDKjdkin;D:Program FilesSunSDKjdkjrein;E:oracleora92in;C:Program

    FilesOraclejre1.3.1in;C:Program FilesOraclejre1.1.8in;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32

    Wbem;C:Program FilesIntelDMIX;C:Program FilesMicrosoft SQL Server90Toolsinn;D:Program FilesCodeSmithv5.0

    ;D:Program FilesSymantecpcAnywhere;C:Program FilesIxiaIxChariot;D:Program FilesSunSDKin;
      USERNAME = gmj
      NUMBER_OF_PROCESSORS = 2
      PROCESSOR_IDENTIFIER = x86 Family 6 Model 23 Stepping 6, GenuineIntel
      _ACP_LIB = D:Program FilesMicrosoft Visual Studio 8VClib;D:Program FilesMicrosoft Visual Studio 8

    VCatlmfclib;D:Program FilesMicrosoft Visual Studio 8VCatlmfclibi386;D:Program FilesMicrosoft Visual Studio 8

    VCPlatformSDKlib;D:Program FilesMicrosoft Visual Studio 8SDKv2.0lib;D:Program FilesMicrosoft Visual Studio 8

    ;D:Program FilesMicrosoft Visual Studio 8lib
      WecVersionForRosebud.DA8 = 2
      SystemRoot = C:WINDOWS
      ComSpec = C:WINDOWSsystem32cmd.exe
      LOGONSERVER = http://www.cnblogs.com/xinyuxin912/***
      VS80COMNTOOLS = D:Program FilesMicrosoft Visual Studio 8Common7Tools
      _ACP_ATLPROV = D:Program FilesMicrosoft Visual Studio 8VCBinATLProv.dll
      WV_GATEWAY_CFG = E:oracleora92Apachemodplsqlcfgwdbsvr.app
      CommonProgramFiles = C:Program FilesCommon Files
      PROCESSOR_LEVEL = 6
      PROCESSOR_REVISION = 1706
      CLIENTNAME = Console
      ALLUSERSPROFILE = C:Documents and SettingsAll Users
      VS90COMNTOOLS = D:Program FilesMicrosoft Visual Studio 9.0Common7Tools
      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;  // WindowsStart MenuPrograms
    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 MenuPrograms
    constint CSIDL_CONTROLS                                          =0x0003;             // My ComputerControl Panel
    constint CSIDL_PRINTERS                                          =0x0004;             // My ComputerPrinters
    constint CSIDL_PERSONAL                                          =0x0005;             // My Documents
    constint CSIDL_FAVORITES                                       =0x0006;               // <user name>Favorites
    constint CSIDL_STARTUP                                 =0x0007;               // Start MenuProgramsStartup
    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> ethood
    constint CSIDL_FONTS                                      =0x0014;            // windowsfonts
    constint CSIDL_TEMPLATES                                  =0x0015;
    constint CSIDL_COMMON_STARTMENU                        =0x0016;               // All UsersStart Menu
    constint CSIDL_COMMON_PROGRAMS                  =0x0017;              // All UsersStart MenuPrograms
    constint CSIDL_COMMON_STARTUP                    =0x0018;             // All UsersStartup
    constint CSIDL_COMMON_DESKTOPDIRECTORY  =0x0019;              // All UsersDesktop
    constint CSIDL_APPDATA                                  =0x001a;              // <user name>Application Data
    constint CSIDL_PRINTHOOD                                  =0x001b;            // <user name>PrintHood
    constint CSIDL_LOCAL_APPDATA                      =0x001c;            // <user name>Local SettingsApplicaiton 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 UsersApplication 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 FilesMy 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 FilesCommon
    constint CSIDL_PROGRAM_FILES_COMMONX86  =0x002c;              // x86 Program FilesCommon on RISC
    constint CSIDL_COMMON_TEMPLATES                        =0x002d;               // All UsersTemplates
    constint CSIDL_COMMON_DOCUMENTS                        =0x002e;               // All UsersDocuments
    constint CSIDL_COMMON_ADMINTOOLS                  =0x002f;            // All UsersStart MenuProgramsAdministrative Tools
    constint CSIDL_ADMINTOOLS                                =0x0030;             // <user name>Start MenuProgramsAdministrative Tools
    constint CSIDL_CONNECTIONS                              =0x0031;              // Network and Dial-up Connections
    constint CSIDL_COMMON_MUSIC                            =0x0035;               // All UsersMy Music
    constint CSIDL_COMMON_PICTURES                  =0x0036;              // All UsersMy Pictures
    constint CSIDL_COMMON_VIDEO                            =0x0037;               // All UsersMy 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;              // USERPROFILELocal SettingsApplication DataMicrosoftCD 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 SettingsAll Users「开始」菜单程序",则可以直接使用

    string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms);

     没实际验证过,如果您验证了帮我补充一

    Example:

    // C:UsersUSERNAMEDocuments

    string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

  • 相关阅读:
    C++Primer第7章 类
    网络编程I/O复用
    bg和fg
    I/O复用的高级应用一:非阻塞connect
    C++Primer第6章 函数
    mvn archetype:createfromproject报错The specified user settings file does not exist
    C++Primer第13章 拷贝控制
    g++编译多线程
    .NET开发者必备的19个开发工具
    jQuery UI Datepicker繁體中文年月選單換行的小修正
  • 原文地址:https://www.cnblogs.com/bluestorm/p/3282502.html
Copyright © 2020-2023  润新知