[VC2005] ドライブやフォルダのプロパティダイアログボックスを表示する方法

◆概要
この資料は、Microsoft(R) Visual C# 2005で ドライブやフォルダのプロパティダイアログボックスを表示する方法について記述しています。

◆ShellExecuteEx()関数を使ってドライブのプロパティを表示する APIのShellExecuteEx関数を使えばドライブやファイルのプロパティダイアログボックスを表示することができます。

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;//for DLL

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

        [StructLayout(LayoutKind.Sequential)]
        public class SHELLEXECUTEINFO
        {
            public int cbSize;
            public int fMask;
            public int hwnd;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpVerb;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpFile;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpParameters;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpDirectory;
            public int nShow;
            public int hInstApp;
            public int lpIDList;
            public string lpClass;
            public int hkeyClass;
            public int dwHotKey;
            public int hIcon;
            public int hProcess;
        }

        const int SEE_MASK_INVOKEIDLIST = 0x0000000c;

        #region Shell32.dll functions

        [DllImport("Shell32.dll", CharSet = CharSet.Auto)]
        public static extern int ShellExecuteEx(SHELLEXECUTEINFO shinfo);

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            SHELLEXECUTEINFO shinfo = new SHELLEXECUTEINFO();
            shinfo.cbSize = Marshal.SizeOf(typeof(SHELLEXECUTEINFO));
            shinfo.fMask = SEE_MASK_INVOKEIDLIST;
            shinfo.lpVerb = "Properties";
            shinfo.hwnd = (int)this.Handle;
            shinfo.lpParameters = null;
            shinfo.lpDirectory = null;
            shinfo.lpFile = @"c:\";
            ShellExecuteEx(shinfo);
        }
    }
}

◆実行結果

ドライブのプロパティダイアログボックス

なお、ファイルのパス(上記の例では、shinfo.lpFile = @"c:\";)にファイルへの絶対パスを記述すれば、ファイルのプロパティダイアログボックスが表示されます。



▼ページトップへ