by kirupa |
7 August 2007
In the previous
page, you got a brief overview of what the Open File dialog window is, and
you downloaded the source
files needed to create our sample application.
The Open File dialog window provides an Explorer-like view for navigating
through your files and selecting a file for you to open. What is returned after
your journey through the Open File dialog window is the path to the file you
selected.
To use this dialog window, you use the
OpenFileDialog class. Copy and paste the following two lines into your
WindowLauncher method:
- private void
WindowLauncher(object
sender, RoutedEventArgs e)
- {
- OpenFileDialog
ofd = new
OpenFileDialog();
- ofd.ShowDialog();
- }
Once you have added the above two lines to your WindowLauncher method, run
your application by press F5. Click on your Launch Window button to see your
Open File dialog appear.
If you look at our code, we declare an OpenFileDialog object called
ofd. We then call ofd's
ShowDialog() method:
- ofd.ShowDialog();
When you use the ShowDialog() method, you are
launching a window that is modal. A modal window borrows focus from your entire
application to itself. You will be unable to access the window that spawned the
modal window until your modal window is closed.
One of the primary functions of your Open File dialog window is to return the
path of the file you selected. To retrieve the path of the selected file,
replace the code inside your WindowLauncher with the following code:
- private void
WindowLauncher(object
sender, RoutedEventArgs e)
- {
- OpenFileDialog
ofd = new
OpenFileDialog();
-
- if (ofd.ShowDialog()
== true)
- {
- string
filePath =
ofd.FileName;
- string
safeFilePath =
ofd.SafeFileName;
- }
- }
This time, if you select a file by double-clicking on it or by selecting it
and pressing your Open button, the file's name is stored in two variables called
filePath and safeFilePath. Before we get to the file names itself, notice how I
check to make sure that a file has been specified for opening:
- if (ofd.ShowDialog()
== true)
- {
- string filePath
= ofd.FileName;
- string safeFilePath
= ofd.SafeFileName;
- }
Your ShowDialog() method returns an object of type
bool? which is a nullable boolean. A nullable
boolean returns a value that is true,
false, and a third
type that is null. If you select a file for
Open, your ShowDialog method returns true. If you click Cancel or close your
Open File dialog window, the value is false.
Since we are interested in dealing with the result of a
file that is specified for opening, we only check the true property. Now, let's
look at our filePath and safeFilePath variables:
- string filePath
= ofd.FileName;
- string safeFilePath
= ofd.SafeFileName;
The filePath variable gets its data by accessing ofd's FileName property, and
safeFilePath gets its data by accessing ofd's SafeFileName property. Your
FileName is the full path to your file plust your file's name and extension. The
SafeFileName returns just the name of your file and its extension. The path is
not returned.
For example, let's say you decide to open a file located in your C:\Windows
path called regedit:
[ opening a file using the Open File dialog window ]
When you select that file for opening, your FileName will be
C:\Windows\regedit.exe, and your SafeFileName will be
regedit.exe. Depending on what you are doing, you will need to decide
whether you want to use FileName or SafeFileName.
In the
next page, let's take what we've done a step further and
look at opening multiple files at once.
Onwards to the
next page!
|