[C#][Windows Formsアプリ][OpenFileDialog] Multiselect プロパティで複数ファイル選択を可能にする

スポンサーリンク

はじめに

Windows Forms アプリケーションで OpenFileDialog を使用する際、ユーザーに複数のファイルを同時に選択させたい場合があります。そんな時に役立つのが Multiselect プロパティです。この記事では、OpenFileDialog の Multiselect プロパティについて、初心者の方にもわかりやすく解説します。Multiselect プロパティを使うことで、ファイル選択ダイアログで複数のファイルを選択できるようになり、アプリケーションの利便性を向上させることができます。具体的なコード例を交えながら、その使い方を見ていきましょう。

Multiselect プロパティとは?

Multiselect プロパティは、OpenFileDialog でユーザーが複数のファイルを選択できるかどうかを指定するプロパティです。

  • true
    ユーザーは複数のファイルを選択できます。選択されたファイルの一覧は、OpenFileDialog.FileNames プロパティから取得できます。
  • false (既定値):
    ユーザーは単一のファイルしか選択できません。

Multiselect プロパティの使い方 (コード例)

Multiselect を有効にする

Multiselect プロパティを true に設定することで、複数ファイル選択を有効にできます。

using System;
using System.Windows.Forms;

namespace OpenFileDialogSample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "ファイルを開く";
            openFileDialog.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            // Multiselect を有効にする
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 選択されたファイルのパスをすべて取得
                string[] filePaths = openFileDialog.FileNames;

                // 選択されたファイルを表示
                string message = "選択されたファイル:\n";
                foreach (string filePath in filePaths)
                {
                    message += filePath + "\n";
                }
                MessageBox.Show(message);
            }
        }
    }
}
実行例

実行例

実行例

実行例

  • openFileDialog.Multiselect = true;
    Multiselect プロパティを true に設定することで、OpenFileDialog で複数のファイルを選択できるようになります。
  • string[] filePaths = openFileDialog.FileNames;
    OpenFileDialog.FileNames プロパティから、選択されたすべてのファイルのパスを文字列配列として取得します。
  • foreach (string filePath in filePaths)
    取得したファイルパスをループ処理で一つずつ取り出し、メッセージボックスに表示します。

選択されたファイル名の取得

Multiselect が true の場合、選択されたファイルの名前 (パスを含まない) は OpenFileDialog.SafeFileNames プロパティから取得できます。

 

private void openButton_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Title = "ファイルを開く";
    openFileDialog.Filter = "テキストファイル(*.txt)|*.txt|すべてのファイル(*.*)|*.*";
    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    // Multiselect を有効にする
    openFileDialog.Multiselect = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        // 選択されたファイルの名前をすべて取得
        string[] fileNames = openFileDialog.SafeFileNames;

        // 選択されたファイルの名前を表示
        string message = "選択されたファイル名:\n";
        foreach (string fileName in fileNames)
        {
            message += fileName + "\n";
        }
        MessageBox.Show(message);
    }
}

解説:

string[] fileNames = openFileDialog.SafeFileNames;: OpenFileDialog.SafeFileNames プロパティから、選択されたすべてのファイルの名前を文字列配列として取得します。パスは含まれません。

まとめ

OpenFileDialog の Multiselect プロパティは、ユーザーに複数のファイルを同時に選択させるための簡単な方法です。この記事で紹介したコード例を参考に、Multiselect プロパティを効果的に活用し、より利便性の高い Windows Forms アプリケーションを作成してください。

Please follow and like us:

コメント

タイトルとURLをコピーしました