英語で読む

次の方法で共有


FileDialog.FileNames プロパティ

定義

ダイアログ ボックスで選択されたすべてのファイルの名前を取得します。

[System.ComponentModel.Browsable(false)]
public string[] FileNames { get; }

プロパティ値

String[]

ダイアログ ボックスで選択されたすべてのファイルの名前を含む String 型の配列。

属性

次のコード例では、ユーザーが複数の画像を選択し、フォーム上のコントロールに PictureBox 表示できるようにします。 を初期化し、 プロパティと Filter プロパティをOpenFileDialog設定Titleし、 プロパティを true に設定してユーザーが複数のファイルを選択できるようにする方法をMultiselect示します。 このコード例では、フォームに 、 という名前の OpenFileDialog コントロール、および という名前SelectFileButtonopenFileDialog1Button コントロールが既にFlowLayoutPanelflowLayoutPanel1存在することを前提としています。

private void Form1_Load(object sender, EventArgs e)
{
    InitializeOpenFileDialog();
}

private void InitializeOpenFileDialog()
{
    // Set the file dialog to filter for graphics files.
    this.openFileDialog1.Filter =
        "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
        "All files (*.*)|*.*";

    // Allow the user to select multiple images.
    this.openFileDialog1.Multiselect = true;
    this.openFileDialog1.Title = "My Image Browser";
}

private void selectFilesButton_Click(object sender, EventArgs e)
{
    DialogResult dr = this.openFileDialog1.ShowDialog();
    if (dr == System.Windows.Forms.DialogResult.OK)
    {
        // Read the files
        foreach (String file in openFileDialog1.FileNames) 
        {
            // Create a PictureBox.
            try
            {
                PictureBox pb = new PictureBox();
                Image loadedImage = Image.FromFile(file);
                pb.Height = loadedImage.Height;
                pb.Width = loadedImage.Width;
                pb.Image = loadedImage;
                flowLayoutPanel1.Controls.Add(pb);
            }
            catch (SecurityException ex)
            {
                // The user lacks appropriate permissions to read files, discover paths, etc.
                MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
                    "Error message: " + ex.Message + "\n\n" +
                    "Details (send to Support):\n\n" + ex.StackTrace
                );
            }
            catch (Exception ex)
            {
                // Could not load the image - probably related to Windows file system permissions.
                MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
                    + ". You may not have permission to read the file, or " +
                    "it may be corrupt.\n\nReported error: " + ex.Message);
            }
        }
    }

注釈

各ファイル名には、ファイル パスと拡張子の両方が含まれます。 ファイルが選択されていない場合、このメソッドは空の配列を返します。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください