[C#][Windows Formsアプリ][OpenFileDialog] Filtrerプロパティを使用して表示するファイルの種類を絞り込む

スポンサーリンク

はじめに

Windows Forms アプリケーションで OpenFileDialog を使う際、特定の種類のファイルだけを表示させたい場合があります。そんな時に役立つのが Filter プロパティです。この記事では、OpenFileDialog の Filter プロパティについて、初心者の方にもわかりやすく解説します。Filter プロパティを使うことで、ユーザーがファイル選択ダイアログで選択できるファイルの種類を絞り込むことができます。具体的なコード例を交えながら、その使い方を見ていきましょう。

Filter プロパティとは?

Filter プロパティは、OpenFileDialog に表示するファイルの種類を制限するためのプロパティです。これを使用することで、特定の拡張子を持つファイルだけを表示したり、複数のファイル形式をまとめて表示したりすることができます。

Filter プロパティの構文

Filter プロパティは、以下の形式の文字列で設定します。

"表示名1|拡張子1|表示名2|拡張子2|..."
  • 表示名: ファイル選択ダイアログに表示されるファイルの種類の説明 (例: “テキストファイル”)
  • 拡張子: 表示名に対応するファイルの拡張子 (例: “*.txt”)
  • | (パイプ): 表示名と拡張子、および複数のフィルタを区切るための記号

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

単一のファイル形式を表示する

最も基本的な使い方は、特定の拡張子を持つファイルだけを表示することです。

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 = "ファイルを開く";

            // テキストファイル (*.txt) のみを表示
            openFileDialog.Filter = "テキストファイル(*.txt)|*.txt";

            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                MessageBox.Show("選択されたファイル: " + filePath);
            }
        }
    }
}
実行例

実行例

  • openFileDialog.Filter = “テキストファイル(*.txt)|*.txt”;
    Filter プロパティに “テキストファイル(*.txt)|*.txt” を設定することで、ファイル選択ダイアログには拡張子が .txt のファイルだけが表示されます。

複数のファイル形式を表示する

複数のファイル形式を表示するには、|

private void openButton_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Title = "ファイルを開く";

    // テキストファイル (*.txt) と CSVファイル (*.csv) を表示
    openFileDialog.Filter = "テキストファイル(*.txt)|*.txt|CSVファイル(*.csv)|*.csv";

    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        string filePath = openFileDialog.FileName;
        MessageBox.Show("選択されたファイル: " + filePath);
    }
}
実行例

実行例


解説:

openFileDialog.Filter = “テキストファイル(*.txt)|*.txt|CSVファイル(*.csv)|*.csv”;
Filter プロパティに “テキストファイル(*.txt)|*.txt|CSVファイル(*.csv)|*.csv” を設定することで、ファイル選択ダイアログには拡張子が .txt と .csv のファイルが表示されます。ダイアログの下部にあるファイルの種類選択リストには、「テキストファイル(.txt)」と「CSVファイル(.csv)」が表示されます。

すべてのファイルを表示する

すべてのファイルを表示するには、”*.*” を使用します。

private void openButton_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Title = "ファイルを開く";

    // すべてのファイルを表示
    openFileDialog.Filter = "すべてのファイル(*.*)|*.*";

    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        string filePath = openFileDialog.FileName;
        MessageBox.Show("選択されたファイル: " + filePath);
    }
}

解説:

openFileDialog.Filter = “すべてのファイル(*.*)|*.*”;
Filter プロパティに “すべてのファイル(*.*)|*.*” を設定することで、ファイル選択ダイアログにはすべてのファイルが表示されます。

まとめ

OpenFileDialog の Filter プロパティは、ファイル選択ダイアログに表示するファイルの種類を絞り込むための非常に便利なプロパティです。この記事で紹介したコード例を参考に、Filter プロパティを効果的に活用し、ユーザーにとって使いやすい Windows Forms アプリケーションを作成してください。

Please follow and like us:

コメント

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