How can I get the current user directory?

前端 未结 9 1003
说谎
说谎 2020-11-27 13:33

Using this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

I get this output:

\"C:\\\\Documents and S         


        
相关标签:
9条回答
  • 2020-11-27 14:12

    You can get the UserProfile path with just this:

    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

    0 讨论(0)
  • 2020-11-27 14:16

    Also very helpful, while investigating the Environment.SpecialFolder enum. Use LINQPad or create a solution and execute this code:

    Enum.GetValues(typeof(Environment.SpecialFolder))
        .Cast<Environment.SpecialFolder>()
        .Select(specialFolder => new
        {
            Name = specialFolder.ToString(),
            Path = Environment.GetFolderPath(specialFolder)
        })
        .OrderBy(item => item.Path.ToLower())
    

    This is the result on my machine:

    MyComputer
    LocalizedResources
    CommonOemLinks
    ProgramFiles            C:\Program Files (x86) 
    ProgramFilesX86         C:\Program Files (x86) 
    CommonProgramFiles      C:\Program Files (x86)\Common Files 
    CommonProgramFilesX86   C:\Program Files (x86)\Common Files 
    CommonApplicationData   C:\ProgramData 
    CommonStartMenu         C:\ProgramData\Microsoft\Windows\Start Menu 
    CommonPrograms          C:\ProgramData\Microsoft\Windows\Start Menu\Programs 
    CommonAdminTools        C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools 
    CommonStartup           C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup 
    CommonTemplates         C:\ProgramData\Microsoft\Windows\Templates 
    UserProfile             C:\Users\fisch 
    LocalApplicationData    C:\Users\fisch\AppData\Local 
    CDBurning               C:\Users\fisch\AppData\Local\Microsoft\Windows\Burn\Burn 
    History                 C:\Users\fisch\AppData\Local\Microsoft\Windows\History 
    InternetCache           C:\Users\fisch\AppData\Local\Microsoft\Windows\INetCache 
    Cookies                 C:\Users\fisch\AppData\Local\Microsoft\Windows\INetCookies 
    ApplicationData         C:\Users\fisch\AppData\Roaming 
    NetworkShortcuts        C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Network Shortcuts 
    PrinterShortcuts        C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Printer Shortcuts 
    Recent                  C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Recent 
    SendTo                  C:\Users\fisch\AppData\Roaming\Microsoft\Windows\SendTo 
    StartMenu               C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu 
    Programs                C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs 
    AdminTools              C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools 
    Startup                 C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 
    Templates               C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Templates 
    Desktop                 C:\Users\fisch\Desktop 
    DesktopDirectory        C:\Users\fisch\Desktop 
    Favorites               C:\Users\fisch\Favorites 
    MyMusic                 C:\Users\fisch\Music 
    MyDocuments             C:\Users\fisch\OneDrive\Documents 
    MyDocuments             C:\Users\fisch\OneDrive\Documents 
    MyPictures              C:\Users\fisch\OneDrive\Pictures 
    MyVideos                C:\Users\fisch\Videos 
    CommonDesktopDirectory  C:\Users\Public\Desktop 
    CommonDocuments         C:\Users\Public\Documents 
    CommonMusic             C:\Users\Public\Music 
    CommonPictures          C:\Users\Public\Pictures 
    CommonVideos            C:\Users\Public\Videos 
    Windows                 C:\Windows 
    Fonts                   C:\Windows\Fonts 
    Resources               C:\Windows\resources 
    System                  C:\Windows\system32 
    SystemX86               C:\Windows\SysWoW64 
    

    ("fisch" is the first 5 letters of my last name. This is the user name assigned when signing in with a Microsoft Account.)

    0 讨论(0)
  • 2020-11-27 14:17

    Messing around with environment variables or hard-coded parent folder offsets is never a good idea when there is a API to get the info you want, call SHGetSpecialFolderPath(...,CSIDL_PROFILE,...)

    0 讨论(0)
提交回复
热议问题