[VC2005] 特殊フォルダのパスを取得する方法[API]

◆概要
この資料は、Microsoft(R) Visual C# 2005で APIを使って特殊フォルダのパスを取得する方法について記述しています。

◆SHGetSpecialFolderPath()関数を使って特殊フォルダのパスを取得する

APIのSHGetSpecialFolderPath関数を使えば、「スタート メニュー」「送る」「最近使ったファイル」などの特殊フォルダのパスを取得することができます。 特殊フォルダのパスを取得するには、EnvironmentクラスのGetFolderPathメソッドを使う方法もありますが、FontフォルダなどSHGetSpecialFolderPath関数でなければ取得できないものもあります。

SHGetSpecialFolderPath API 呼び出しに適切な特殊フォルダ定数を渡すことによって、任意の特殊フォルダのフルパスを表示できます。

◆Sample

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //APIの宣言
        [DllImport("shell32.dll")]
        static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,
           [Out] StringBuilder lpszPath, int nFolder, bool fCreate);

        //API関数で使う特殊フォルダ定数(使うものだけの記述でよい)
        const int CSIDL_DESKTOP = 0x0000;
        const int CSIDL_FAVORITES = 0x0006;
        const int CSIDL_FONTS = 0x0014;
        const int CSIDL_PERSONAL = 0x0005;
        const int CSIDL_PROGRAM_FILES = 0x0026;
        const int CSIDL_PROGRAMS = 0x0002;
        const int CSIDL_STARTUP = 0x0007;
        const int CSIDL_WINDOWS = 0x0024;
        const int CSIDL_NETHOOD = 0x0013;
        const int CSIDL_CONTROLS = 0x0003;
        const int CSIDL_DRIVES = 0x0011;
        const int CSIDL_RECENT = 0x0008;
        const int CSIDL_SENDTO = 0x0009;
        const int CSIDL_APPDATA = 0x001a;
        const int CSIDL_TEMPLATES = 0x0015;
        const int CSIDL_COMMON_STARTMENU = 0x0016;
        const int CSIDL_COMMON_PROGRAMS = 0X0017;
        const int CSIDL_COMMON_STARTUP = 0x0018;
        const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;
        const int CSIDL_COMMON_APPDATA = 0x0023;
        const int CSIDL_COMMON_TEMPLATES = 0x002d;
        const int CSIDL_COMMON_DOCUMENTS = 0x002e;
        const int CSIDL_COMMON_MUSIC = 0x0035;
        const int CSIDL_COMMON_PICTURES = 0x0036;
        const int CSIDL_COMMON_VIDEO = 0x0037;
        const int CSIDL_COMMON_FAVORITES = 0x001f;
        const int CSIDL_COMMON_ADMINTOOLS = 0x002f;
        const int CSIDL_ADMINTOOLS = 0x0030;
        const int CSIDL_MYMUSIC = 0x000d;
        const int CSIDL_MYVIDEO = 0x000e;
        const int CSIDL_LOCAL_APPDATA = 0x001c;
        const int CSIDL_SYSTEM = 0x0025;
        const int CSIDL_PROGRAM_FILES_COMMON = 0x002b;
        const int CSIDL_PROFILE = 0x0028;
        const int CSIDL_MYPICTURES = 0x0027;
        const int CSIDL_INTERNET = 0x0001;

        private string getPath(int folderCSIDL)
        {
            StringBuilder resultPath = new StringBuilder(255);

            SHGetSpecialFolderPath((IntPtr)0, resultPath, 
                folderCSIDL, false);
            return resultPath.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Fontフォルダ
            string spath = getPath(CSIDL_FONTS);
            MessageBox.Show(spath);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //User-My Documentsフォルダ
            string spath = getPath(CSIDL_PERSONAL);
            MessageBox.Show(spath);
        }

        private void btnDeskTop_Click(object sender, EventArgs e)
        {
            //DESKTOP
            string spath = getPath(CSIDL_DESKTOP);
            MessageBox.Show(spath);
        }

        private void btnFAVORITES_Click(object sender, EventArgs e)
        {
            //お気に入り
            string spath = getPath(CSIDL_FAVORITES);
            MessageBox.Show(spath);
        }

        private void btnPROGRAMFILES_Click(object sender, EventArgs e)
        {
            //Program Files
            string spath = getPath(CSIDL_PROGRAM_FILES);
            MessageBox.Show(spath);
        }

        private void btn_PROGRAMS_Click(object sender, EventArgs e)
        {
            //<Documents and Settings>\<user name>スタートメニュー\プログラム
            string spath = getPath(CSIDL_PROGRAMS);
            MessageBox.Show(spath);
        }
    }
}

◆特殊フォルダ定数とパス

特殊フォルダ定数
(=以下は宣言部のみ記述)
パス
CSIDL_DESKTOP = 0x0000C:\Documents and Settings\<User Name>\デスクトップ
CSIDL_INTERNET = 0x0001デスクトップ上のInternet Exploreのアイコン
CSIDL_FAVORITES = 0x0006C:\Documents and Settings\<User Name>\Favorites
CSIDL_FONTS = 0x0014C:\WINDOWS\Fonts
CSIDL_PERSONAL = 0x0005C:\Documents and Settings\<User Name>\My Documents
CSIDL_PROGRAM_FILES = 0x0026C:\Program Files
CSIDL_PROGRAMS = 0x0002C:\Documents and Settings\<User Name>\スタート メニュー\プログラム
CSIDL_STARTUP = 0x0007C:\Documents and Settings\<User Name>\スタート メニュー\プログラム\スタートアップ
CSIDL_WINDOWS = 0x0024C:\WINDOWS
CSIDL_NETHOOD = 0x0013C:\Documents and Settings\<User Name>\NetHood(共有フォルダ)
CSIDL_CONTROLS = 0x0003My Computer\Control Panel
CSIDL_DRIVES = 0x0011My Computer
CSIDL_RECENT = 0x0008C:\Documents and Settings\<User Name>\Recent
CSIDL_SENDTO = 0x0009C:\Documents and Settings\<User Name>\SendTo
CSIDL_APPDATA = 0x001aC:\Documents and Settings\<User Name>\Application Data
CSIDL_TEMPLATES = 0x0015C:\Documents and Settings\<User Name>\Templates
CSIDL_COMMON_STARTMENU = 0x0016C:\Documents and Settings\All Users\スタート メニュー
CSIDL_COMMON_PROGRAMS = 0X0017C:\Documents and Settings\All Users\スタート メニュー\プログラム
CSIDL_COMMON_STARTUP = 0x0018C:\Documents and Settings\All Users\スタート メニュー\プログラム\スタートアップ
CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019C:\Documents and Settings\All Users\デスクトップ
CSIDL_COMMON_APPDATA = 0x0023C:\Documents and Settings\All Users\Application Data
CSIDL_COMMON_TEMPLATES = 0x002dC:\Documents and Settings\All Users\Templates
CSIDL_COMMON_DOCUMENTS = 0x002e C:\Documents and Settings\All Users\Documents
CSIDL_COMMON_MUSIC = 0x0035 C:\Documents and Settings\All Users\Documents\My Music
CSIDL_COMMON_PICTURES = 0x0036 C:\Documents and Settings\All Users\Documents\My Pictures
CSIDL_COMMON_VIDEO = 0x0037C:\Documents and Settings\All Users\Documents\My Videos
CSIDL_COMMON_FAVORITES = 0x001f C:\Documents and Settings\All Users\Favorites
CSIDL_COMMON_ADMINTOOLS = 0x002f C:\Documents and Settings\All Users\スタート メニュー\プログラム\管理ツール
CSIDL_ADMINTOOLS = 0x0030 <user name>\Start Menu\Programs\Administrative Tools
CSIDL_MYMUSIC = 0x000d C:\Documents and Settings\<User Name>\My Documents\My Music
CSIDL_MYVIDEO = 0x000e C:\Documents and Settings\<User Name>\My Documents\My Videos
CSIDL_LOCAL_APPDATA = 0x001c C:\Documents and Settings\<User Name>\Local Settings\Application Data
CSIDL_SYSTEM = 0x0025 C:\WINDOWS\system32
CSIDL_PROGRAM_FILES_COMMON = 0x002b C:\Program Files\Common Files
CSIDL_PROFILE = 0x0028 C:\Documents and Settings\<User Name>
CSIDL_MYPICTURES = 0x0027 C:\Documents and Settings\<User Name>\My Documents\My Pictures


◆サンプルダウンロード
ダウンロード  SHGetSpecialFolderPath.zip

▼ページトップへ