DOBON.NET プログラミング道: .NET Framework, VB.NET, C#, Visual Basic, Visual Studio, インストーラ, ...

特殊ディレクトリのパスを取得する

EnvironmentクラスのGetFolderPathメソッドにより、デスクトップ、お気に入り、スタートメニュー、システムディレクトリなどなどの特殊ディレクトリの絶対パスを取得することができます。

補足:システムディレクトリはEnvironment.SystemDirectoryプロパティでも取得できます。また、現在のディレクトリはEnvironment.CurrentDirectoryプロパティにより取得します。一時ファイル名、一時ディレクトリ名の取得に関しては、こちらをご覧ください。
Environment.SpecialFolder列挙体のメンバ 説明 具体例
ApplicationData 現在のローミングユーザーのApplication Dataフォルダ
C:\Documents and Settings\UserName\Application Data
CommonApplicationData すべてのユーザーのApplication Dataフォルダ
C:\Documents and Settings\All Users\Application Data
CommonProgramFiles 共有ファイルフォルダ
C:\Program Files\Common Files
Cookies クッキーフォルダ
C:\Documents and Settings\UserName\Cookies
Desktop 論理的なデスクトップ(.NET Framework 1.1以降)
C:\Documents and Settings\UserName\デスクトップ
DesktopDirectory デスクトップ C:\Documents and Settings\UserName\デスクトップ
Favorites お気に入り
C:\Documents and Settings\UserName\Favorites
History 履歴
C:\Documents and Settings\UserName\Local Settings\History
InternetCache インターネットキャッシュ
C:\Documents and Settings\UserName\Local Settings\Temporary Internet Files
LocalApplicationData 現在の非ローミングユーザーのApplication Dataフォルダ
C:\Documents and Settings\UserName\Local Settings\Application Data
MyComputer マイ コンピュータ(.NET Framework 1.1以降)
 
MyDocuments マイ ドキュメント(.NET Framework 2.0以降)
C:\Documents and Settings\UserName\My Documents
MyMusic マイ ミュージック(.NET Framework 1.1以降) C:\Documents and Settings\UserName\My Documents\My Music
MyPictures マイ ピクチャ(.NET Framework 1.1以降) C:\Documents and Settings\UserName\My Documents\My Pictures
Personal マイドキュメント
C:\Documents and Settings\UserName\My Documents
ProgramFiles プログラムファイル
C:\Program Files
Programs スタートメニューのプログラム
C:\Documents and Settings\UserName\スタート メニュー\プログラム
Recent 最近使用したドキュメント
C:\Documents and Settings\UserName\Recent
SendTo 「送る」フォルダ
C:\Documents and Settings\UserName\SendTo
StartMenu スタートメニュー
C:\Documents and Settings\UserName\スタート メニュー
Startup スタートアップ
C:\Documents and Settings\UserName\スタート メニュー\プログラム\スタートアップ
System ウィンドウズシステムフォルダ
C:\WINDOWS\System32
Templates テンプレート
C:\Documents and Settings\UserName\Templates
VB.NET
コードを隠すコードを選択
'共有ファイルフォルダ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles))
'結果: C:\Program Files\Common Files

'クッキーフォルダ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies))
'結果: C:\Documents and Settings\UserName\Cookies

'デスクトップ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
'結果: C:\Documents and Settings\UserName\デスクトップ

'お気に入り
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites))
'結果: C:\Documents and Settings\UserName\Favorites

'履歴
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.History))
'結果: C:\Documents and Settings\UserName\Local Settings\History

'インターネットキャッシュ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
'結果: C:\Documents and Settings\UserName\Local Settings\Temporary Internet Files

'マイドキュメント
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.Personal))
'結果: C:\Documents and Settings\UserName\My Documents

'プログラムファイル
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))
'結果: C:\Program Files

'スタートメニュー
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu))
'結果: C:\Documents and Settings\UserName\スタート メニュー

'スタートメニューのプログラム
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.Programs))
'結果: C:\Documents and Settings\UserName\スタート メニュー\プログラム

'スタートアップ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.Startup))
'結果: C:\Documents and Settings\UserName\スタート メニュー\プログラム\スタートアップ

'最近使用したドキュメント
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.Recent))
'結果: C:\Documents and Settings\UserName\Recent

'「送る」フォルダ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.SendTo))
'結果: C:\Documents and Settings\UserName\SendTo

'ウィンドウズシステムフォルダ
'System.Environment.SystemDirectoryでも可
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.System))
'結果: C:\WINDOWS\System32

'テンプレート
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.Templates))
'結果: C:\Documents and Settings\UserName\Templates

'すべてのユーザーのApplication Dataフォルダ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
'結果: C:\Documents and Settings\All Users\Application Data

'現在のローミングユーザーのApplication Dataフォルダ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
'結果: C:\Documents and Settings\UserName\Application Data

'現在の非ローミングユーザーのApplication Dataフォルダ
Console.WriteLine( _
    System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
'結果: C:\Documents and Settings\UserName\Local Settings\Application Data
C#
コードを隠すコードを選択
//共有ファイルフォルダ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles));
//結果: C:\Program Files\Common Files

//クッキーフォルダ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
//結果: C:\Documents and Settings\UserName\Cookies

//デスクトップ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
//結果: C:\Documents and Settings\UserName\デスクトップ

//お気に入り
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
//結果: C:\Documents and Settings\UserName\Favorites

//履歴
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.History));
//結果: C:\Documents and Settings\UserName\Local Settings\History

//インターネットキャッシュ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
//結果: C:\Documents and Settings\UserName\Local Settings\Temporary Internet Files

//マイドキュメント
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.Personal));
//結果: C:\Documents and Settings\UserName\My Documents

//プログラムファイル
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
//結果: C:\Program Files

//スタートメニュー
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
//結果: C:\Documents and Settings\UserName\スタート メニュー

//スタートメニューのプログラム
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.Programs));
//結果: C:\Documents and Settings\UserName\スタート メニュー\プログラム

//スタートアップ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.Startup));
//結果: C:\Documents and Settings\UserName\スタート メニュー\プログラム\スタートアップ

//最近使用したドキュメント
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.Recent));
//結果: C:\Documents and Settings\UserName\Recent

//「送る」フォルダ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.SendTo));
//結果: C:\Documents and Settings\UserName\SendTo

//ウィンドウズシステムフォルダ
//System.Environment.SystemDirectoryでも可
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.System));
//結果: C:\WINDOWS\System32

//テンプレート
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.Templates));
//結果: C:\Documents and Settings\UserName\Templates

//すべてのユーザーのApplication Dataフォルダ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
//結果: C:\Documents and Settings\All Users\Application Data

//現在のローミングユーザーのApplication Dataフォルダ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
//結果: C:\Documents and Settings\UserName\Application Data

//現在の非ローミングユーザーのApplication Dataフォルダ
Console.WriteLine(
    System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
//結果: C:\Documents and Settings\UserName\Local Settings\Application Data
  • 履歴:
  • 2007/1/25 .NET Framework 2.0に関する記述を追加。

注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。

  • Windows Vista以降でUACが有効になっていると、ファイルへの書き込みに失敗する可能性があります。詳しくは、こちらをご覧ください。