471,338 Members | 1,044 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

OpenFileDialog and SaveFileDialog

Hello!

The object called dlgOpenFile is of type OpenFileDialog.

In this example the eventhandler OnFileOpen is called when the Open File in
a menu is clicked.
Here we call the OpenFileDialog and select a filename to be opened and then
if the Open
button is selected we call the OpenFile method.

Now to my question: I just wonder when is the file opened. Is it at the same
time as
the static method File.ReadAllText(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

//Tony
Jul 8 '08 #1
3 2693
Thats correct. That functions opens the file, reads all the text from it and
closes it again. The OpenFileDialog just returns a file name to you by
default and doesnt open the file. It does have functions on it to Open the
file and return a stream but that would mean you would have to write the code
to read the contents of the file.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Tony Johansson" wrote:
Hello!

The object called dlgOpenFile is of type OpenFileDialog.

In this example the eventhandler OnFileOpen is called when the Open File in
a menu is clicked.
Here we call the OpenFileDialog and select a filename to be opened and then
if the Open
button is selected we call the OpenFile method.

Now to my question: I just wonder when is the file opened. Is it at the same
time as
the static method File.ReadAllText(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

//Tony
Jul 8 '08 #2
Neither OpenFileDialog, nor SaveFileDialog actually open a file. The
filename returned is for you to use in code after opening. ReadAllText will
open and close the file as part of its code.

"Tony Johansson" wrote:
Hello!

The object called dlgOpenFile is of type OpenFileDialog.

In this example the eventhandler OnFileOpen is called when the Open File in
a menu is clicked.
Here we call the OpenFileDialog and select a filename to be opened and then
if the Open
button is selected we call the OpenFile method.

Now to my question: I just wonder when is the file opened. Is it at the same
time as
the static method File.ReadAllText(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

//Tony
Jul 8 '08 #3
Hello Tony,

it is opend while during executing File.ReadAllText(filename);

The FileOpenDialog only returns the Filename (as string) of the
selected file an thus does not open it.
Jul 9 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ken Allen | last post: by
11 posts views Thread by Rasmus Teglgaard | last post: by
4 posts views Thread by NickP | last post: by
17 posts views Thread by Peter Duniho | last post: by
3 posts views Thread by Martijn Mulder | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.