Open File Dialog with file types : Open File Dialog « GUI Windows Form « C# / C Sharp






Open File Dialog with file types

Open File Dialog with file types
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

namespace nsClassLib
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    
    public class clsMainOpenFileDialog
    {
        [STAThread]
        static public void Main ()
        {
            OpenFileDialog ofn = new OpenFileDialog ();
            ofn.Filter = "C Sharp Files (*.cs)|*.cs|Text Files (*.txt)|*.txt";
            ofn.Title = "Type File";
            while (true)
            {
                if (ofn.ShowDialog () == DialogResult.Cancel)
                    return;
                FileStream strm;
                try
                {
                    strm = new FileStream (ofn.FileName, FileMode.Open, FileAccess.Read);
                    StreamReader rdr = new StreamReader (strm);
                    while (rdr.Peek() >= 0)
                    {
                         string str = rdr.ReadLine ();
                         Console.WriteLine (str);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show ("Error opening file", "File Error",
                                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                ofn.Title = "Next File to Type";
            }
        }
    }
}


           
       








Related examples in the same category

1.Call ShowDialog() to display an OpenFileDialog
2.Set InitialDirectory
3.Set CheckFileExists to true
4.Set Filter
5.FileOk Action
6.shows browsing for a set of filesshows browsing for a set of files
7.shows browsing for a fileshows browsing for a file
8.Demonstrates using an OpenFileDialog to prompt for a file name, and to open a fileDemonstrates using an OpenFileDialog to prompt for a file name, and to open a file
9.Show the use of some of the OpenFile dialog boxShow the use of some of the OpenFile dialog box