在 Vista 上,GetFolderPath (... CommonApplicationData)仍然返回“ C: 文档和设置”

据我所知,你应该使用 Environment.GetFolderPath方法,这样你就有了独立于操作系统的代码..。

WindowsXP 使用 C:\Documents and Settings\,WindowsVista 使用 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);
}
131190 次浏览

My installer copied a log.txt file which had been generated on an XP computer. I was looking at that log file thinking it was generated on Vista. Once I fixed my log4net configuration to be "Vista Compatible". Environment.GetFolderPath was returning the expected results. Therefore, I'm closing this post.

The following SpecialFolder path reference might be useful:

Output On 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`

Output on 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

Output on Ubuntu 9.10 -> Ubuntu 12.04 with mono 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

Output on Ubuntu 16.04 with mono 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:

where $USER is the current user

Output on Ubuntu 16.04 using dotnet core (3.0.100)

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

Output on Android 6 using Xamarin 7.2

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:

Where $APPNAME is the name of your Xamarin application (eg. MyApp.Droid)

Output on iOS Simulator 10.3 using Xamarin 7.2

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:

Where $DEVICEGUID is the simulator GUID (depending on the selected simulator)

Output on ipad 10.3 using Xamarin 7.2

SpecialFolder.MyDocuments: /var/mobile/Containers/Data/Application/$APPLICATIONGUID/Documents

Output on ipad 13.3 using Xamarin 16.4

SpecialFolder.MyDocuments: /var/mobile/Containers/Data/Application/$APPLICATIONGUID/Documents
SpecialFolder.UserProfile: /private/var/mobile/Containers/Data/Application/$APPLICATIONGUID/Documents

Output on windows 10 using .net core 3.1

SpecialFolder.MyDocuments: C:\Users\$USER\Documents

Output on Ubuntu 18.04 using .net core 3.1

SpecialFolder.MyDocuments: /home/$USER

Output on MacOS Catalina using .net core 3.1

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

Output on Snap Package - Core 18 using .net core 3.1

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

(Note: I found that if a snap environment if /home/$USER/snap/$APPNAME/x$VERSION/.local/share exists then LocalApplicationData will return that path. However if the path doesn't exist it returns empty string.

Output on 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:

Code Snippet if you want to log your own

foreach(Environment.SpecialFolder f in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
string commonAppData = Environment.GetFolderPath(f);
Console.WriteLine("{0}: {1}", f, commonAppData);
}
Console.ReadLine();

Output on Windows 7 (64-bit)

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

Output on 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

I was looking for a listing of macOS but found nothing, maybe this helps someone.

Output on macOS Catalina (10.15.7) using net5.0

# SpecialFolders (Only with value)
SpecialFolder.ApplicationData: /Users/$USER/.config
SpecialFolder.CommonApplicationData: /usr/share
SpecialFolder.Desktop: /Users/$USER/Desktop
SpecialFolder.DesktopDirectory: /Users/$USER/Desktop
SpecialFolder.Favorites: /Users/$USER/Library/Favorites
SpecialFolder.Fonts: /Users/$USER/Library/Fonts
SpecialFolder.InternetCache: /Users/$USER/Library/Caches
SpecialFolder.LocalApplicationData: /Users/$USER/.local/share
SpecialFolder.MyDocuments: /Users/$USER
SpecialFolder.MyMusic: /Users/$USER/Music
SpecialFolder.MyPictures: /Users/$USER/Pictures
SpecialFolder.ProgramFiles: /Applications
SpecialFolder.System: /System
SpecialFolder.UserProfile: /Users/$USER


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

I have replaced my username with $USER.

Code Snippet from pogosama.

foreach(Environment.SpecialFolder f in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
string commonAppData = Environment.GetFolderPath(f);
Console.WriteLine("{0}: {1}", f, commonAppData);
}
Console.ReadLine();