特殊ディレクトリのパスを取得するEnvironmentクラスのGetFolderPathメソッドにより、デスクトップ、お気に入り、スタートメニュー、システムディレクトリなどなどの特殊ディレクトリの絶対パスを取得することができます。
補足:システムディレクトリはEnvironment.SystemDirectoryプロパティでも取得できます。また、現在のディレクトリはEnvironment.CurrentDirectoryプロパティにより取得します。一時ファイル名、一時ディレクトリ名の取得に関しては、こちらをご覧ください。
'共有ファイルフォルダ 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 //共有ファイルフォルダ 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
なお、.NET Framework 2.0以降のVB.NETでは、My.Computer.FileSystem.SpecialDirectoriesのプロパティにより、特殊ディレクトリの一部を取得することもできます。
注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。
|
|
Copyright(C) DOBON!. All rights reserved.
|