4 题: Environment.GetFolderPath(... CommonApplicationData)仍然在Vista上返回“C:\ Documents and Settings \”

在...创建的问题 Thu, Feb 21, 2013 12:00 AM

据我所知,您应该使用Environment.GetFolderPath方法,因此您拥有与操作系统无关的代码......

Windows XP使用C:\Documents and Settings\。 Windows Vista使用C:\ProgramDataC:\Users

我在Windows Vista计算机上使用下面的代码,它正在返回一个C:\Documents and Settings\目录,而不是C:\ProgramData,就像它应该......任何想法?

 
    string commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    try
    {
        File.CreateText(
            Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
            "\\mycompany\\uid");
        log.Debug("Created file successfully");
    }
    catch (Exception ex)
    {
        log.Error("Unable to create the uid file: ", ex);
    }
    
72
  1. 是否从XP更新了Vista安装?
    2009-05-21 23:29:09Z
  2. 不,当它从商店带回家时它肯定有Vista ...我猜它有可能被bestbuy或者某些东西“升级”(似乎不太可能) 。 c:\文件和设置根本不存在,如果它告诉你什么。
    2009-05-22 16:34:46Z
  3. 如何将“\\mycompany \\uid”解析为路径?我想生成这个确切的路径(使用company& ui)。我是否必须从程序集中手动添加此数据?
    2011-06-18 14:04:40Z
  4. 醇>
    4个答案                              4 跨度>                         

    我的安装程序复制了一个在XP计算机上生成的log.txt文件。我正在查看该日志文件,认为它是在Vista上生成的。一旦我修复我的log4net配置为“Vista兼容”。 Environment.GetFolderPath返回了预期的结果。因此,我正在关闭这篇文章。

    以下SpecialFolder路径引用可能很有用:

    Windows Server 2003上的输出:

     
    SpecialFolder.ApplicationData: C:\Documents and Settings\blake\Application Data
    SpecialFolder.CommonApplicationData: C:\Documents and Settings\All Users\Application Data
    SpecialFolder.ProgramFiles: C:\Program Files
    SpecialFolder.CommonProgramFiles: C:\Program Files\Common Files
    SpecialFolder.DesktopDirectory: C:\Documents and Settings\blake\Desktop
    SpecialFolder.LocalApplicationData: C:\Documents and Settings\blake\Local Settings\Application Data
    SpecialFolder.MyDocuments: C:\Documents and Settings\blake\My Documents
    SpecialFolder.System: C:\WINDOWS\system32`
    

    Vista上的输出:

     
    SpecialFolder.ApplicationData: C:\Users\blake\AppData\Roaming
    SpecialFolder.CommonApplicationData: C:\ProgramData
    SpecialFolder.ProgramFiles: C:\Program Files
    SpecialFolder.CommonProgramFiles: C:\Program Files\Common Files
    SpecialFolder.DesktopDirectory: C:\Users\blake\Desktop
    SpecialFolder.LocalApplicationData: C:\Users\blake\AppData\Local
    SpecialFolder.MyDocuments: C:\Users\blake\Documents
    SpecialFolder.System: C:\Windows\system32
    
        
    152
    2009-05-22 21:51:46Z
    1. 为什么log4net配置与Environment.GetFolderPath有关?至于“任何想法?”,是的,你得到第一个结果作为普通用户,第二个结果作为管理员运行,例如运行提升的安装程序或服务时。
      2017-01-19 03:34:14Z
    2. 此外,我认为读取所有这些列表的新手应该知道这些不一定是操作系统上的固定路径,但几乎可以在“你的”机器上。永远不要硬编码 - 而是使用GetFolderPath,或者有一天你会后悔的。哦,并且使用Path.Combine而不是plus运算符。
      2017-01-19 03:43:42Z
    3. 由于某种原因,wondows10,Environment.SpecialFolder.CommonApplicationData上的
      返回CommonApplicationData。因此在应用程序路径本身创建文件夹
      2018-02-24 10:27:48Z
    4. @ mrid Environment.SpecialFolder.CommonApplicationData是一个枚举;你需要拨打Environment.GetFolderPath()来返回路径,例如Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
      2018-05-29 17:39:53Z
    5. 醇>

    Ubuntu 9.10上的输出 - > Ubuntu 12.04,单声道2.10.8.1:

     
    SpecialFolder.ApplicationData: /home/$USER/.config
    SpecialFolder.CommonApplicationData: /usr/share
    SpecialFolder.ProgramFiles: 
    SpecialFolder.DesktopDirectory: /home/$USER/Desktop
    SpecialFolder.LocalApplicationData: /home/$USER/.local/share
    SpecialFolder.MyDocuments: /home/$USER
    SpecialFolder.System: 
    
    SpecialFolder.Personal: /home/$USER
    

    在Ubuntu 16.04上输出单声道4.2.1

     
    SpecialFolder.ApplicationData: /home/$USER/.config
    SpecialFolder.CommonApplicationData: /usr/share
    SpecialFolder.ProgramFiles:
    SpecialFolder.DesktopDirectory: /home/$USER/Desktop
    SpecialFolder.LocalApplicationData: /home/$USER/.local/share
    SpecialFolder.MyDocuments: /home/$USER
    SpecialFolder.Desktop: /home/$USER/Desktop
    SpecialFolder.Personal: /home/$USER
    
    SpecialFolder.System: 
    SpecialFolder.Programs: 
    SpecialFolder.Favorites: 
    SpecialFolder.Startup: 
    SpecialFolder.Recent: 
    SpecialFolder.SendTo: 
    SpecialFolder.StartMenu: 
    SpecialFolder.MyMusic: /home/$USER/Music
    SpecialFolder.MyVideos: /home/$USER/Videos
    SpecialFolder.MyComputer: 
    SpecialFolder.NetworkShortcuts: 
    SpecialFolder.Fonts: /home/$USER/.fonts
    SpecialFolder.Templates: /home/$USER/Templates
    SpecialFolder.CommonStartMenu: 
    SpecialFolder.CommonPrograms: 
    SpecialFolder.CommonStartup: 
    SpecialFolder.CommonDesktopDirectory: 
    SpecialFolder.PrinterShortcuts: 
    SpecialFolder.InternetCache: 
    SpecialFolder.Cookies: 
    SpecialFolder.History: 
    SpecialFolder.Windows: 
    SpecialFolder.MyPictures: /home/$USER/Pictures
    SpecialFolder.UserProfile: /home/$USER
    SpecialFolder.SystemX86: 
    SpecialFolder.ProgramFilesX86: 
    SpecialFolder.CommonProgramFiles: 
    SpecialFolder.CommonProgramFilesX86: 
    SpecialFolder.CommonTemplates: /usr/share/templates
    SpecialFolder.CommonDocuments: 
    SpecialFolder.CommonAdminTools: 
    SpecialFolder.AdminTools: 
    SpecialFolder.CommonMusic: 
    SpecialFolder.CommonPictures: 
    SpecialFolder.CommonVideos: 
    SpecialFolder.Resources: 
    SpecialFolder.LocalizedResources: 
    SpecialFolder.CommonOemLinks: 
    SpecialFolder.CDBurning: 
    

    其中$USER是当前用户

    使用Xamarin 7.2在Android 6上输出

     
    Environment.SpecialFolder.ApplicationData: /data/user/0/$APPNAME/files/.config
    Environment.SpecialFolder.CommonApplicationData: /usr/share
    Environment.SpecialFolder.ProgramFiles: 
    Environment.SpecialFolder.DesktopDirectory: /data/user/0/$APPNAME/files/Desktop
    Environment.SpecialFolder.LocalApplicationData: /data/user/0/$APPNAME/files/.local/share
    Environment.SpecialFolder.MyDocuments: /data/user/0/$APPNAME/files
    Environment.SpecialFolder.Desktop: /data/user/0/$APPNAME/files/Desktop
    Environment.SpecialFolder.Personal: /data/user/0$APPNAME/files
    
    Environment.SpecialFolder.Startup: 
    Environment.SpecialFolder.Recent: 
    Environment.SpecialFolder.SendTo: 
    Environment.SpecialFolder.StartMenu: 
    Environment.SpecialFolder.MyMusic: /data/user/0/$APPNAME/files/Music
    Environment.SpecialFolder.MyVideos: /data/user/0/$APPNAME/files/Videos
    Environment.SpecialFolder.MyComputer: 
    Environment.SpecialFolder.NetworkShortcuts: 
    Environment.SpecialFolder.Fonts: /data/user/0/$APPNAME/files/.fonts
    Environment.SpecialFolder.Templates: /data/user/0/$APPNAME/files/Templates
    Environment.SpecialFolder.CommonStartMenu: 
    Environment.SpecialFolder.CommonPrograms: 
    Environment.SpecialFolder.CommonStartup: 
    Environment.SpecialFolder.CommonDesktopDirectory: 
    Environment.SpecialFolder.PrinterShortcuts: 
    Environment.SpecialFolder.InternetCache: 
    Environment.SpecialFolder.Cookies: 
    Environment.SpecialFolder.History: 
    Environment.SpecialFolder.Windows: 
    Environment.SpecialFolder.MyPictures: /data/user/0/$APPNAME/files/Pictures
    Environment.SpecialFolder.UserProfile: /data/user/0/$APPNAME/files
    Environment.SpecialFolder.SystemX86: 
    Environment.SpecialFolder.ProgramFilesX86: 
    Environment.SpecialFolder.CommonProgramFiles: 
    Environment.SpecialFolder.CommonProgramFilesX86: 
    Environment.SpecialFolder.CommonTemplates: /usr/share/templates
    Environment.SpecialFolder.CommonDocuments: 
    Environment.SpecialFolder.CommonAdminTools: 
    Environment.SpecialFolder.AdminTools: 
    Environment.SpecialFolder.CommonMusic: 
    Environment.SpecialFolder.CommonPictures: 
    Environment.SpecialFolder.CommonVideos: 
    Environment.SpecialFolder.Resources: 
    Environment.SpecialFolder.LocalizedResources: 
    Environment.SpecialFolder.CommonOemLinks: 
    Environment.SpecialFolder.CDBurning: 
    

    其中$APPNAME是您的Xamarin应用程序的名称(例如MyApp.Droid)

    使用Xamarin 7.2在iOS模拟器10.3上输出

     
    ApplicationData: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/.config
    CommonApplicationData: /usr/share
    ProgramFiles: /Applications
    DesktopDirectory: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/Desktop
    LocalApplicationData: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents
    MyDocuments: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents
    Desktop: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/Desktop
    MyDocuments: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents
    Startup: 
    Recent: 
    SendTo: 
    StartMenu: 
    MyMusic: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/Music
    MyVideos: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/Videos
    MyComputer: 
    NetworkShortcuts: 
    Fonts: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/.fonts
    Templates: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/Templates
    CommonStartMenu: 
    CommonPrograms: 
    CommonStartup: 
    CommonDesktopDirectory: 
    PrinterShortcuts: 
    InternetCache: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Library/Caches
    Cookies: 
    History: 
    Windows: 
    MyPictures: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Documents/Pictures
    UserProfile: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID
    SystemX86: 
    ProgramFilesX86: 
    CommonProgramFiles: 
    CommonProgramFilesX86: 
    CommonTemplates: /usr/share/templates
    CommonDocuments: 
    CommonAdminTools: 
    AdminTools: 
    CommonMusic: 
    CommonPictures: 
    CommonVideos: 
    Resources: /Users/$USER/Library/Developer/CoreSimulator/Devices/$DEVICEGUID/data/Containers/Data/Application/$APPLICATIONGUID/Library
    LocalizedResources: 
    CommonOemLinks: 
    CDBurning: 
    

    其中$DEVICEGUID是模拟器GUID(取决于所选的模拟器)

    使用Xamarin 7.2在ipad 10.3上输出

     
    SpecialFolder.MyDocuments: /var/mobile/Containers/Data/Application/$APPLICATIONGUID/Documents
    
        
    30
    2017-06-20 15:15:43Z

    Windows 10上的输出

     
    Fonts: C:\Windows\Fonts
    CommonStartMenu: C:\ProgramData\Microsoft\Windows\Start Menu
    CommonPrograms: C:\ProgramData\Microsoft\Windows\Start Menu\Programs
    CommonStartup: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
    CommonDesktopDirectory: C:\Users\Public\Desktop
    CommonApplicationData: C:\ProgramData
    Windows: C:\Windows
    System: C:\Windows\system32
    ProgramFiles: C:\Program Files (x86)
    SystemX86: C:\Windows\SysWOW64
    ProgramFilesX86: C:\Program Files (x86)
    CommonProgramFiles: C:\Program Files (x86)\Common Files
    CommonProgramFilesX86: C:\Program Files (x86)\Common Files
    CommonTemplates: C:\ProgramData\Microsoft\Windows\Templates
    CommonDocuments: C:\Users\Public\Documents
    CommonAdminTools: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools
    CommonMusic: C:\Users\Public\Music
    CommonPictures: C:\Users\Public\Pictures
    CommonVideos: C:\Users\Public\Videos
    Resources: C:\Windows\resources
    LocalizedResources:
    CommonOemLinks:
    

    代码段,如果您想要自己记录

     
    foreach(Environment.SpecialFolder f in Enum.GetValues(typeof(Environment.SpecialFolder)))
    {
        string commonAppData = Environment.GetFolderPath(f);
        Console.WriteLine("{0}: {1}", f, commonAppData);
    }
    Console.ReadLine();
    
        
    18
    2016-10-13 14:00:52Z

    Windows 7(64位)上的输出

     
    SpecialFolder.CommonApplicationData: C:\ProgramData 
    SpecialFolder.CommonDesktopDirectory: C:\Users\Public\Desktop
    SpecialFolder.CommonStartMenu: C:\ProgramData\Microsoft\Windows\Start Menu
    SpecialFolder.CommonPrograms: C:\ProgramData\Microsoft\Windows\Start Menu\Programs
    SpecialFolder.CommonProgramFiles: C:\Program Files\Common Files
    SpecialFolder.CommonProgramFilesX86: C:\Program Files (x86)\Common Files
    SpecialFolder.CommonStartup: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
    SpecialFolder.ProgramFiles: C:\Program Files
    SpecialFolder.ProgramFilesX86: C:\Program Files (x86)
    SpecialFolder.System: C:\Windows\system32
    SpecialFolder.SystemX86: C:\Windows\SysWOW64
    

    Windows XP上的输出

     
    SpecialFolder.CommonApplicationData: C:\Documents and Settings\All Users\Application Data
    SpecialFolder.CommonDesktopDirectory: C:\Documents and Settings\All Users\Desktop
    SpecialFolder.CommonPrograms: C:\Documents and Settings\All Users\Start Menu\Programs
    SpecialFolder.CommonProgramFiles: C:\Program Files\Common Files
    SpecialFolder.CommonProgramFilesX86:
    SpecialFolder.CommonStartMenu: C:\Documents and Settings\All Users\Start Menu
    SpecialFolder.CommonStartup: C:\Documents and Settings\All Users\Start Menu\Programs\Startup
    SpecialFolder.ProgramFiles: C:\Program Files
    SpecialFolder.ProgramFilesX86:
    SpecialFolder.System: C:\WINDOWS\system32
    SpecialFolder.SystemX86: C:\WINDOWS\system32
    
        
    8
    2016-10-13 13:39:22Z
来源放置 这里