public bool OpenFileDialog() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Загрузка изображения"; openFileDialog.DefaultExt = "JPEG(*.jpg; *.jpeg)| *.jpg"; openFileDialog.Filter = "Все поддерживаемые форматы | *.png; *.jpg; *.jpeg; *.bmp; *.gif"; List <string> allowableFileTypes = new List <string>(); allowableFileTypes.AddRange(new string[] { ".png", ".jpg", ".jpeg", ".bmp", ".gif" }); if (openFileDialog.ShowDialog() == true) { if (!openFileDialog.Equals(string.Empty)) { FileInfo f = new FileInfo(openFileDialog.FileName); if (allowableFileTypes.Contains(f.Extension.ToLower())) { // Место положение открытого файла FilePath = openFileDialog.FileName; return(true); } else { MessageBox.Show("Этот формат файла не поддерживается!"); } } else { MessageBox.Show("Укажите файл для открытия!"); } } return(false); }
public void DirectorySelection(object sender, EventArgs e) { var button = (Button)sender; Debug.WriteLine(button.Name); switch (button.Name) { case "CmdSelectDirectory": { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.ShowDialog(); if (dialog.Equals(DialogResult.OK) && String.IsNullOrEmpty(dialog.SelectedPath)) { MessageBox.Show("Folder specified is not valid or does not exist, plase try again or type in manually.", "Error"); return; } TxtDirectory.Text = dialog.SelectedPath; TxtJobName.Text = Path.GetFileNameWithoutExtension(dialog.SelectedPath) + " "; BaseDirectory = TxtJobName.Text; UpdateLog("Directory changed to (" + dialog.SelectedPath + ")"); break; } case "CmdATSelect": case "CmdTemplateSelect": { OpenFileDialog dialog = new OpenFileDialog { CheckPathExists = true, Multiselect = false, Title = "Please select a drawing template file." }; if (button.Name.Equals("CmdTemplateSelect")) { if (!String.IsNullOrEmpty(Properties.Settings.Default.TemplateFile)) { dialog.FileName = Properties.Settings.Default.TemplateFile; } dialog.Filter = "AutoCAD Drawing|*.dwg|All Files|*.*"; } else { if (!String.IsNullOrEmpty(Properties.Settings.Default.AssetTableFile)) { dialog.FileName = Properties.Settings.Default.AssetTableFile; } dialog.Filter = "Excel File|*.xlsx;*.xls|All Files|*.*"; } dialog.ShowDialog(); if (dialog.Equals(DialogResult.OK) && String.IsNullOrEmpty(dialog.FileName)) { MessageBox.Show("File specified is not valid or does not exist, plase try again or type in manually.", "Error"); return; } if (button.Name.Equals("CmdTemplateSelect")) { TxtCADTemplate.Text = dialog.FileName; Properties.Settings.Default.TemplateFile = dialog.FileName; Properties.Settings.Default.Save(); UpdateLog("AutoCAD Template file changed to " + Path.GetFileName(dialog.FileName) + " (" + dialog.FileName + ")"); } else { TxtATTemplate.Text = dialog.FileName; Properties.Settings.Default.AssetTableFile = dialog.FileName; Properties.Settings.Default.Save(); UpdateLog("Asset Table Template file changed to " + Path.GetFileName(dialog.FileName) + " (" + dialog.FileName + ")"); } break; } } }