Csharp/CSharp Tutorial/GUI Windows Forms/OpenFileDialog

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

OpenFileDialog: browsing for a file

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);
    }
  }
}

OpenFileDialog: browsing for a set of files

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);
    }
  }
}

OpenFileDialog Event: FileOk

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);
    }
}

OpenFileDialog: filer, initial directory

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);
            
        }
    }
}

Simple Open file dialog

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);
    }
  }
}