Csharp/CSharp Tutorial/GUI Windows Forms/OpenFileDialog
Содержание
OpenFileDialog: browsing for a file
<source lang="csharp">using System; using System.Windows.Forms; public class OpenFileDialogDisplay {
public static void Main() { OpenFileDialog dlgOpen = new OpenFileDialog(); if (dlgOpen.ShowDialog() == DialogResult.OK){ Console.Write(dlgOpen.FileName); } }
}</source>
OpenFileDialog: browsing for a set of files
<source lang="csharp">using System; using System.Windows.Forms; public class OpenFileDialogSetFiles {
public static void Main() { OpenFileDialog dlgOpen = new OpenFileDialog(); dlgOpen.Title = "Select one or more files"; dlgOpen.ShowReadOnly = true; dlgOpen.Multiselect = true; if (dlgOpen.ShowDialog() == DialogResult.OK) { foreach (string s in dlgOpen.FileNames) Console.WriteLine(s); } }
}</source>
OpenFileDialog Event: FileOk
<source lang="csharp">using System; using System.Drawing; using System.ruponentModel; using System.Windows.Forms; using System.IO; public class OpenFileDialogEvent{
static OpenFileDialog openfiledlg1; public static void Main() { openfiledlg1 = new OpenFileDialog(); openfiledlg1.FileOk += new CancelEventHandler(OnFileOpenOK); openfiledlg1.Filter = "C# files (*.cs)|*.cs|Bitmap files (*.bmp)|*.bmp"; openfiledlg1.FilterIndex = 1; openfiledlg1.ShowDialog(); } static void OnFileOpenOK(Object sender, CancelEventArgs e) { MessageBox.Show("You selected the file "+openfiledlg1.FileName); }
}</source>
OpenFileDialog: filer, initial directory
<source lang="csharp">using System; using System.Collections.Generic; using System.Windows.Forms; static class MainClass {
[STAThread] static void Main() { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Rich Text Files (*.rtf)|*.RTF|" + "All files (*.*)|*.*"; dlg.CheckFileExists = true; dlg.InitialDirectory = Application.StartupPath; if (dlg.ShowDialog() == DialogResult.OK) { Console.WriteLine(dlg.FileName); } }
}</source>
Simple Open file dialog
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; public class OpenFileDialogTest{
public static void Main() { OpenFileDialog dlg=new OpenFileDialog(); if(dlg.ShowDialog() == DialogResult.OK) { MessageBox.Show("You selected the file "+dlg.FileName); } }
}</source>