Открывая несколько файлов (OpenFileDialog, C#)

Синтаксис, в целом, делает язык трудно для изучения, уже не говоря о ведущем устройстве. Поэтому, чем меньший набор синтаксиса, тем легче изучить и попытаться освоить. Это - основная причина, почему много новых языков заимствуют синтаксис из популярных, существующих языков.

кроме того, в то время как я могу просто постараться не изучать определенные функции, я не интересуюсь по любой причине, я в конечном счете прочитаю чужой код, кто делает как эта функция, и затем я должен буду пойти, узнают что функция только для понимания их кода.

29
задан jay_t55 6 August 2014 в 16:50
поделиться

1 ответ

Необходимо установить для свойства OpenFileDialog.Multiselect значение true, а затем получить доступ к свойству OpenFileDialog.FileNames .

Проверьте этот пример

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);
            }
        }
    }
69
ответ дан 28 November 2019 в 01:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: