Set InitialDirectory

image_pdfimage_print
   
 

using System;
using System.Windows.Forms;

class MainClass {
    static void Main(string[] args) {

        OpenFileDialog dlg = new OpenFileDialog();
        dlg.InitialDirectory = Application.StartupPath;

        if (dlg.ShowDialog() == DialogResult.OK) {
            Console.WriteLine(dlg.FileName);

        }
    }
}

    


Set Filter

image_pdfimage_print
   
 
using System;
using System.Windows.Forms;

class MainClass {
    static void Main(string[] args) {

        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Filter = "Rich Text Files (*.rtf)|*.RTF|" +
          "All files (*.*)|*.*";
        if (dlg.ShowDialog() == DialogResult.OK) {
            Console.WriteLine(dlg.FileName);

        }
    }
}

    


FileOk Action

image_pdfimage_print
   
 
using System;
using System.IO;
using System.Windows.Forms;

class FileDialogApp {
    private static OpenFileDialog ofd;

    static void Main(string[] args) {
        ofd = new OpenFileDialog();
        string s = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));

        ofd.InitialDirectory = s;
        ofd.FileOk += new System.ComponentModel.CancelEventHandler(ofd_OK);
        ofd.ShowDialog();
    }
    public static void ofd_OK(object sender,
        System.ComponentModel.CancelEventArgs e) {
        StreamReader sr = new StreamReader(ofd.FileName);
        string s;
        while ((s = sr.ReadLine()) != null) {
            Console.WriteLine(s);
        }
        sr.Close();
    }
}

    


Yes No Cancel

image_pdfimage_print
   
 
using System;
using System.Windows.Forms;

public class MainClass {

    public static void Main() {
        switch (MessageBox.Show("Do you want to create a new file?",
                            "WonderWord",
                            MessageBoxButtons.YesNoCancel,
                            MessageBoxIcon.Question)) {
            case DialogResult.Yes:
                // "Yes" processing
                break;

            case DialogResult.No:
                // "No" processing
                break;

            case DialogResult.Cancel:
                // "Cancel" processing
                break;
        }
    }
}

    


Display Question messagebox and check its result

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Test{
    [STAThread]
    static void Main() 
    {
            DialogResult reply = MessageBox.Show("Question?",
            "Yes or No Demo",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
      
            if (reply == DialogResult.Yes){
               MessageBox.Show("Yes");
            } else {
               MessageBox.Show("No");
            }
    }
}