特殊フォルダのパスの取得

アプリケーション データ

共通フォルダ

アプリケーション データの共通フォルダは、Environment.GetFolderPath()で取得できます。

public static string GetFolderPath(
    Environment.SpecialFolder folder
    )
Environment.GetFolderPath メソッド (Environment.SpecialFolder) (System) | MSDN

取得対象とするフォルダは、下表の値をfolderに指定します。

Environment.SpecialFolder 列挙型
列挙子 説明 環境変数表記でのパス※1
AdminTools The file system directory that is used to store administrative tools for an individual user. The Microsoft Management Console (MMC) will save customized consoles to this directory, and it will roam with the user. (Added in the .NET Framework 4)  
ApplicationData The directory that serves as a common repository for application-specific data for the current roaming user. %APPDATA%
CDBurning The file system directory that acts as a staging area for files waiting to be written to a CD. (Added in the .NET Framework 4)  
CommonAdminTools The file system directory that contains administrative tools for all users of the computer. (Added in the .NET Framework 4)  
CommonApplicationData The directory that serves as a common repository for application-specific data that is used by all users. %PROGRAMDATA%
CommonDesktopDirectory The file system directory that contains files and folders that appear on the desktop for all users. This special folder is valid only for Windows NT systems. (Added in the .NET Framework 4)  
CommonDocuments The file system directory that contains documents that are common to all users. This special folder is valid for Windows NT systems, Windows 95, and Windows 98 systems with Shfolder.dll installed. (Added in the .NET Framework 4)  
CommonMusic The file system directory that serves as a repository for music files common to all users. (Added in the .NET Framework 4)  
CommonOemLinks This value is recognized in Windows Vista for backward compatibility, but the special folder itself is no longer used. (Added in the .NET Framework 4)  
CommonPictures The file system directory that serves as a repository for image files common to all users. (Added in the .NET Framework 4)  
CommonProgramFiles The directory for components that are shared across applications.To get the x86 common program files directory on a non-x86 system, use the ProgramFilesX86 member. %COMMONPROGRAMFILES%
CommonProgramFilesX86 The Program Files folder. (Added in the .NET Framework 4) %COMMONPROGRAMFILES(x86)%
CommonPrograms A folder for components that are shared across applications. This special folder is valid only for Windows NT, Windows 2000, and Windows XP systems. (Added in the .NET Framework 4) %PROGRAMDATA%\Microsoft\Windows\Start Menu\Programs
CommonStartMenu The file system directory that contains the programs and folders that appear on the Start menu for all users. This special folder is valid only for Windows NT systems. (Added in the .NET Framework 4)  
CommonStartup The file system directory that contains the programs that appear in the Startup folder for all users. This special folder is valid only for Windows NT systems. (Added in the .NET Framework 4)  
CommonTemplates The file system directory that contains the templates that are available to all users. This special folder is valid only for Windows NT systems. (Added in the .NET Framework 4)  
CommonVideos The file system directory that serves as a repository for video files common to all users. (Added in the .NET Framework 4)  
Cookies The directory that serves as a common repository for Internet cookies.  
Desktop The logical Desktop rather than the physical file system location.  
DesktopDirectory The directory used to physically store file objects on the desktop.  
Favorites The directory that serves as a common repository for the user's favorite items.  
Fonts A virtual folder that contains fonts. (Added in the .NET Framework 4)  
History The directory that serves as a common repository for Internet history items.  
InternetCache The directory that serves as a common repository for temporary Internet files.  
LocalApplicationData The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user. %LOCALAPPDATA%
LocalizedResources The file system directory that contains localized resource data. (Added in the .NET Framework 4)  
MyComputer The My Computer folder.  
MyDocuments The My Documents folder.  
MyMusic The My Music folder.  
MyPictures The My Pictures folder.  
MyVideos The file system directory that serves as a repository for videos that belong to a user. (Added in the .NET Framework 4)  
NetworkShortcuts A file system directory that contains the link objects that may exist in the My Network Places virtual folder. (Added in the .NET Framework 4)  
Personal The directory that serves as a common repository for documents.  
PrinterShortcuts The file system directory that contains the link objects that can exist in the Printers virtual folder. (Added in the .NET Framework 4)  
ProgramFiles The program files directory.On a non-x86 system, passing ProgramFiles to the GetFolderPath method returns the path for non-x86 programs. To get the x86 program files directory on a non-x86 system, use the ProgramFilesX86 member. %PROGRAMFILES%
ProgramFilesX86 The x86 Program Files folder. (Added in the .NET Framework 4) %PROGRAMFILES(X86)%
Programs The directory that contains the user's program groups. %APPDATA%\Microsoft\Windows\Start Menu\Programs
Recent The directory that contains the user's most recently used documents.  
Resources The file system directory that contains resource data. (Added in the .NET Framework 4)  
SendTo The directory that contains the Send To menu items.  
StartMenu The directory that contains the Start menu items.  
Startup The directory that corresponds to the user's Startup program group.  
System The System directory.  
SystemX86 The Windows System folder. (Added in the .NET Framework 4)  
Templates The directory that serves as a common repository for document templates.  
UserProfile The user's profile folder. Applications should not create files or folders at this level; they should put their data under the locations referred to by ApplicationData. (Added in the .NET Framework 4)  
Windows The Windows directory or SYSROOT. This corresponds to the %windir% or %SYSTEMROOT% environment variables. (Added in the .NET Framework 4)  
Environment.SpecialFolder 列挙型 (System) | MSDN

※1 実際は環境変数を表す文字列ではなく、その変数が表す文字列で返される

環境変数で定義されているパスならば、Environment.ExpandEnvironmentVariables()でも取得できます。

ローミング ユーザー (roaming user) / 移動ユーザー

コード例
System.Environment.GetFolderPath(
 System.Environment.SpecialFolder.ApplicationData )
取得例※1
C:\Documents and Settings\username
 \Application Data
同等の環境変数 %APPDATA%
※1 Windows XPの場合

非ローミング ユーザー (non-roaming user)

コード例
System.Environment.GetFolderPath(
 System.Environment.SpecialFolder.LocalApplicationData )
取得例※1
C:\Documents and Settings\username
 \Local Settings\Application Data
同等の環境変数 %LOCALAPPDATA%
※1 Windows XPの場合

UWPでは%APPDATA%などへのアクセスはパッケージごとのディレクトリへリダイレクトされるため、取得したパスと実際のパスが異なることがあります。 一般的なファイル システムの操作 - Windows でパッケージ化されたデスクトップ アプリが動作するしくみについて - MSIX | Microsoft Learn desktop bridge - UWP app does not copy file to AppData folder - Stack Overflow windows 10 - How can I get a consistent path for read/write application in C# apps? - Stack Overflow

フォルダ

ローミング ユーザー (roaming user)

コード例
System.Windows.Forms.Application.UserAppDataPath
取得例※1
C:\Documents and Settings\username
 \Application Data
 \CompanyName\ProductName\ProductVersion
※1 Windows XPの場合 Application.UserAppDataPath Property (System.Windows.Forms) | Microsoft Learn

非ローミング ユーザー (non-roaming user)

コード例
System.Windows.Forms.Application.LocalUserAppDataPath
取得例※1
C:\Documents and Settings\username
 \Local Settings\Application Data
 \CompanyName\ProductName\ProductVersion

ファイル

コード例
using System.Configuration;

ConfigurationManager.OpenExeConfiguration(
  ConfigurationUserLevel.PerUserRoamingAndLocal
  ).FilePath
取得例※1
C:\Documents and Settings\username
 \Local Settings\Application Data
 \CompanyName\ApplicationName_Url_??
 \ProductVersion\user.config
※1 Windows XPの場合 ConfigurationUserLevel Enum (System.Configuration) | Microsoft Learn
Microsoft Learnから検索