473,769 Members | 5,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ReadAllTex t(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(obje ct sender, EventArgs e)
{
if (dlgOpenFile.Sh owDialog() == DialogResult.OK )
{
filename = dlgOpenFile.Fil eName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Cle ar();
textBoxEdit.Tex t = File.ReadAllTex t(filename);
}
catch(IOExcepti on ex)
{
MessageBox.Show (ex.Message, "Simple Editor",
MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);
}
}

//Tony
Jul 8 '08 #1
3 2786
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.ReadAllTex t(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(obje ct sender, EventArgs e)
{
if (dlgOpenFile.Sh owDialog() == DialogResult.OK )
{
filename = dlgOpenFile.Fil eName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Cle ar();
textBoxEdit.Tex t = File.ReadAllTex t(filename);
}
catch(IOExcepti on ex)
{
MessageBox.Show (ex.Message, "Simple Editor",
MessageBoxButto ns.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.ReadAllTex t(filename);
is called
or
is it automatically done in the OpenFileDialog?
private void OnFileOpen(obje ct sender, EventArgs e)
{
if (dlgOpenFile.Sh owDialog() == DialogResult.OK )
{
filename = dlgOpenFile.Fil eName;
OpenFile();
}
}

protected void OpenFile()
{
try
{
textBoxEdit.Cle ar();
textBoxEdit.Tex t = File.ReadAllTex t(filename);
}
catch(IOExcepti on ex)
{
MessageBox.Show (ex.Message, "Simple Editor",
MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);
}
}

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

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
19288
by: Ken Allen | last post by:
I have enocuntered something wierd. If I invoke an instance of the above dialog and elect to save the file in a directory where a file of that name does not exist, then the return is DialogResult.OK, but if I elect to save the file on top of a file with the same name in the same directory, when I press the Save button I get a "do you want to overwrite" dialog -- even if I press OK the SaveFileDialog returns DialogResult.Cancel! How...
3
4037
by: cider123 | last post by:
Everything I've read so far states you can't inherit or extend these dialogs because they're "sealed". I considered building my own custom control, but I haven't been able to find: 1) How to add a "Places Bar" to a form 2) How to hook a Combo Box into System locations like the existing OpenFileDialog / SaveFileDialog offer (ie. "My Recent Documents", "Desktop", etc.)
3
3128
by: josh | last post by:
How do I make it actually save or open a file? It only opens the dialogs. What do I type to get it to save? Here's what I have so far: Public Class frmMainApp Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code "
11
4494
by: Rasmus Teglgaard | last post by:
Hi I need to know how to store the OpenfileDialog.OpenFile() in a memorystream. Do I need to do some kind of conversion before this works I can't write dim mem as new memorystream( mem = OpenfileDialog.Openfile( It says not supported cast or something like that
4
4036
by: NickP | last post by:
Hi there, This is really crazy! 1. Make a folder 2. Put a text file in the folder 3. Run the following code... Dim pop As New OpenFileDialog Using pop
17
8029
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
3
3261
by: Martijn Mulder | last post by:
It strikes me that System.Windows.Forms.OpenFileDialog seems te 'remember' which directory it was in last, even when a new OpenFileDialog-object is created for every access to the file system. This 'persistence' also holds between different invocations of the same program. Even when the .cs file is re-compiled, the OpenFileDialog again opens in the directory where it was directed to the previous time. 1. Is this a 'feature' of...
4
2437
by: Jollywg | last post by:
How do you use a path other than the initialdirectory in a savefiledialog in c#?? Thanks
8
11627
by: Joe Duchtel | last post by:
Hello - I have the following code to detemine a file name when my application is saving a file. The problem is that if the file already exists and I select the Yes button in the "Do you want to replace" dialog, the DialogResult is Cancel instead of OK. Is there something I am missing? Dim lSaveFileDialog As New SaveFileDialog
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.