Implementing File Dialog Boxes and File I/O

The remaining code of interest in this application has to do with file I/O and using File Open and File Save dialog boxes. It demonstrates how the WFC classes simplify the job of locating, opening, reading from, and writing to files. The following is a brief summary of the WFC classes used for doing this.

In the WFC class hierarchy, both the com.ms.wfc.ui.OpenFileDialog and com.ms.wfc.ui.SaveFileDialog classes extend com.ms.wfc.ui.FileDialog. FileDialog extends CommonDialog, which is a wrapper for the Win32 common dialog API. All common dialogs are set up with properties such as setTitle and setFilter, and are run by calling the showDialog method. These dialog boxes enable users to choose a file name for opening or saving a file.

The com.ms.wfc.io package contains stream-based I/O classes. The File class, which extends DataStream, contains methods for file I/O. In the case of the MyNotepad application, all that is needed is to open a file, read all of it into the edit control (or write the contents of the edit control to the file), and then close the file.

In the MyNotepad application, all I/O and file dialog code is in the event handler methods for the Open, Save, and Save As items on the File menu. We’ll look at just one of these, the Open menu event handler, because it encapsulates the common dialog and File I/O functionality. The code for FileMenuOpen_click is:

    private void FileMenuOpen_click(Object sender, Event e)
    {
        // Create an Open File dialog box   
        OpenFileDialog ofd = new OpenFileDialog();
        // Set up filters and options
        ofd.setFilter("Text Docs (*.txt)|*.txt|All Files (*.*)|*.*");
        ofd.setDefaultExt("txt");
        // Run the Open File dialog box
        int OK = ofd.showDialog();
        // Check result of dialog box after it closes
        if (OK == DialogResult.OK) {
            // Retrieve the filename entered 
            fileName = ofd.getFileName();
            // Open a File stream on that filename 
            currentDoc = File.open(fileName); 
            // Retrieve the length of the file
            int ilength = (int)currentDoc.getLength();
            // Read in ANSI characters to edit buffer
            editbox.setText(currentDoc.readStringCharsAnsi(ilength));   
            // Close the file handle
            currentDoc.close();
            fileOpen=true;
            // Set the application's caption  
            this.setText(File.getName(fileName) + " - MyNotepad");
        }
    }

When a user clicks Open on the File menu, the FileMenuOpen_click event handler method is called. The first three lines of code in this method create an OpenFileDialog object and set the filters and extensions used by the dialog box. While these lines are manually coded here, you can do the same thing using the Forms Designer by adding an OpenFileDialog object to the form and setting its properties (the initialization code is then placed in the initForm method).

Finally, the OpenFileDialog.showDialog method is called to open the dialog box. When the dialog box closes, this method returns an integer equivalent to DialogResult.OK if the user clicks the OK button, and DialogResult.Cancel if the user clicks the Cancel button. If OK was clicked, the file name is retrieved from the OpenFileDialog object and passed to the File.open method, which returns a File stream opened on the file as read-write access to the file. File.open is a utility function that does the same thing as creating a File object with the following constructor:

File(fileName, File.OPEN, FileAccess.READWRITE, FileShare.NONE);