여러 파일 열기 (OpenFileDialog, C#)
-
19-09-2019 - |
문제
한 번에 여러 파일을 열려고합니다. OpenFileDialog
, 사용 FileNames
대신에 FileName
. 그러나 MSDN에서도이를 달성하는 방법에 대한 사례를 볼 수 없습니다. 내가 알 수있는 한 - 그것에 대한 문서도 없습니다. 아무도 전에이 일을 한 적이 있습니까?
해결책
당신은 설정해야합니다 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);
}
}
}
제휴하지 않습니다 StackOverflow