Post: C# How to Open and Save
10-30-2011, 07:44 PM #1
Pichu
RIP PICHU.
(adsbygoogle = window.adsbygoogle || []).push({}); How to open and save.

Difficulty [][][][][][][][][][] [1/10]

----------------------------------

You will need to create a new form and create the following:
2 Buttons (Name one "Open" and the other "Save"
1 TextBox (Select Mutliline)
SaveFileDialog
OpenFileDialog
label1 (<Name it FilePath and set it to invisible).

Place them how you please. An example of how mine is setup:

You must login or register to view this content.

Double click on the two buttons (Save, Open)
Add "Using System.IO"

It should look like this now:

    using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace CSharp_Open_and_Save
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void save_Click(object sender, EventArgs e)
{


}


private void open_Click(object sender, EventArgs e)
{


}
}
}


To save the text add the following code:

    [FONT=Consolas]if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)[/FONT][LEFT][FONT=Consolas]    {[/FONT]
[FONT=Consolas] File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);[/FONT][/LEFT]
[FONT=Consolas] }[/FONT]


To open and search for the text add the following code:

    [FONT=Consolas]if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)[/FONT][LEFT][FONT=Consolas]    {
label1.Text = openFileDialog1.FileName;[/FONT]
[FONT=Consolas] textBox1.Text = File.ReadAllText(label1.Text);[/FONT][/LEFT]
[FONT=Consolas] }[/FONT]


Now note, when you go to save the text you will see this:

You must login or register to view this content.

Go to SaveFileDialog and change filter to:
(*.txt)|*.txt|All files (*.*)|*.*

This will mean that when you now save it will give the option of .txt or All Files

You must login or register to view this content.

Now you may do the same for OpenFileDialog to get the same results for searching

That is it.

Download the source:
You must login or register to view this content.

The following 3 users say thank you to Pichu for this useful post:

Chrom3D, Epic?, T_m_b07
10-30-2011, 09:48 PM #2
Epic?
Awe-Inspiring
Originally posted by Sublimity View Post
How to open and save.

Difficulty [][][][][][][][][][] [1/10]

----------------------------------

You will need to create a new form and create the following:
2 Buttons (Name one "Open" and the other "Save"
1 TextBox (Select Mutliline)
SaveFileDialog
OpenFileDialog
label1 (<Name it FilePath and set it to invisible).

Place them how you please. An example of how mine is setup:

You must login or register to view this content.

Double click on the two buttons (Save, Open)
Add "Using System.IO"

It should look like this now:

    using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace CSharp_Open_and_Save
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void save_Click(object sender, EventArgs e)
{


}


private void open_Click(object sender, EventArgs e)
{


}
}
}


To save the text add the following code:

    [FONT=Consolas]if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)[/FONT][LEFT][FONT=Consolas]    {[/FONT]
[FONT=Consolas] File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);[/FONT][/LEFT]
[FONT=Consolas] }[/FONT]


To open and search for the text add the following code:

    [FONT=Consolas]if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)[/FONT][LEFT][FONT=Consolas]    {
label1.Text = openFileDialog1.FileName;[/FONT]
[FONT=Consolas] textBox1.Text = File.ReadAllText(label1.Text);[/FONT][/LEFT]
[FONT=Consolas] }[/FONT]


Now note, when you go to save the text you will see this:

You must login or register to view this content.

Go to SaveFileDialog and change filter to:
(*.txt)|*.txt|All files (*.*)|*.*

This will mean that when you now save it will give the option of .txt or All Files

You must login or register to view this content.

Now you may do the same for OpenFileDialog to get the same results for searching

That is it.

Download the source:
You must login or register to view this content.


Perhaps you want to expand your tutorial to include the use of System.IO (mainly File and Stream-related classes)? That way you don't have to use a File Dialog to open and save files?
10-30-2011, 10:14 PM #3
Pichu
RIP PICHU.
Originally posted by Epic
Perhaps you want to expand your tutorial to include the use of System.IO (mainly File and Stream-related classes)? That way you don't have to use a File Dialog to open and save files?


You stating something like,

Press save it automatically saved the text with an already given name on desktop then if you press load it will fetch it and load it?
10-30-2011, 10:20 PM #4
Epic?
Awe-Inspiring
Originally posted by Sublimity View Post
You stating something like,

Press save it automatically saved the text with an already given name on desktop then if you press load it will fetch it and load it?


I don't know, whatever you'd prefer.

I'm talking about You must login or register to view this content., if you really want to make a tutorial on opening and saving files (and general file handling) you can pretty much solely write about System.IO.File. It has Read and Write methods, but also a bunch of other useful file handling methods. But you could also include stuff on streams if you were so inclined.
10-30-2011, 10:25 PM #5
Pichu
RIP PICHU.
Originally posted by Epic
I don't know, whatever you'd prefer.

I'm talking about You must login or register to view this content., if you really want to make a tutorial on opening and saving files (and general file handling) you can pretty much solely write about System.IO.File. It has Read and Write methods, but also a bunch of other useful file handling methods. But you could also include stuff on streams if you were so inclined.


Ohh ok. Well, I just thought I'd write up something simple real quick for people to use. When I have the time I'll write up something like that for people.
11-16-2011, 10:21 PM #6
Woof
...hmm
Originally posted by Sublimity View Post
You must login or register to view this content.

Nice tutorial for the beginners I guess.
I wouldn't suggest this for use though mainly because of the fact you don't have full control over the stream(s).
Then again I'm old-school I still use binary streaming, then again it really depends on what you are making.

You should do one on binary streams and the way you can control them, I'm sure people would appreciate that.

Oh and just so you know you could have just had:
    

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);


As you already imported window forms class. Winky Winky
Last edited by Woof ; 11-16-2011 at 10:27 PM. Reason: Corrected [php] tag fail.

The following user thanked Woof for this useful post:

x_DaftVader_x
11-17-2011, 12:52 AM #7
Pichu
RIP PICHU.
Originally posted by BFresshh View Post
Nice tutorial for the beginners I guess.
I wouldn't suggest this for use though mainly because of the fact you don't have full control over the stream(s).
Then again I'm old-school I still use binary streaming, then again it really depends on what you are making.

You should do one on binary streams and the way you can control them, I'm sure people would appreciate that.

Oh and just so you know you could have just had:
    

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);


As you already imported window forms class. Winky Winky


Haha, true. It was a quick tutorial I just quickly compiled. :P
12-14-2011, 03:25 PM #8
BuC-ShoTz
TeamMvKâ?¢
i like to use BinaryWriter especially for binary files but it will also write strings to a file.
    
string mystring = @"This is a test of the emergency broadcast system!";
byte[] myBytes = new byte[]{0x00, 0x0A, 0x0B};
BinaryWriter br = new BinaryWriter(new FileStream(@"c:\\myfile.dat", FileMode.OpenOrCreate));//FileMode has more options
br.Write(mystring);
br.Write(myBytes);
br.Close();


keep up the good work
Last edited by BuC-ShoTz ; 12-14-2011 at 03:28 PM.
12-14-2011, 08:34 PM #9
Chrom3D
Big Sister
Originally posted by Sublimity View Post
How to open and save.

Difficulty [][][][][][][][][][] [1/10]

----------------------------------

You will need to create a new form and create the following:
2 Buttons (Name one "Open" and the other "Save"
1 TextBox (Select Mutliline)
SaveFileDialog
OpenFileDialog
label1 (<Name it FilePath and set it to invisible).

Place them how you please. An example of how mine is setup:

You must login or register to view this content.

Double click on the two buttons (Save, Open)
Add "Using System.IO"

It should look like this now:

    using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace CSharp_Open_and_Save
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void save_Click(object sender, EventArgs e)
{


}


private void open_Click(object sender, EventArgs e)
{


}
}
}


To save the text add the following code:

    [FONT=Consolas]if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)[/FONT][LEFT][FONT=Consolas]    {[/FONT]
[FONT=Consolas] File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);[/FONT][/LEFT]
[FONT=Consolas] }[/FONT]


To open and search for the text add the following code:

    [FONT=Consolas]if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)[/FONT][LEFT][FONT=Consolas]    {
label1.Text = openFileDialog1.FileName;[/FONT]
[FONT=Consolas] textBox1.Text = File.ReadAllText(label1.Text);[/FONT][/LEFT]
[FONT=Consolas] }[/FONT]


Now note, when you go to save the text you will see this:

You must login or register to view this content.

Go to SaveFileDialog and change filter to:
(*.txt)|*.txt|All files (*.*)|*.*

This will mean that when you now save it will give the option of .txt or All Files

You must login or register to view this content.

Now you may do the same for OpenFileDialog to get the same results for searching

That is it.

Download the source:
You must login or register to view this content.


Good tutorial Sublimity =D

Decided to go from VB.net to C# now. Going read the C# Programming For The Absolute Beginner by Andy Harris. Hopefully it's good Winky Winky

The following user thanked Chrom3D for this useful post:

Pichu
12-19-2011, 09:56 PM #10
Pichu
RIP PICHU.
Originally posted by Chrom3D View Post
Good tutorial Sublimity =D

Decided to go from VB.net to C# now. Going read the C# Programming For The Absolute Beginner by Andy Harris. Hopefully it's good Winky Winky


Happy

I'm thinking I'm just going to stay with C# for now. I'm going to be learning Java in Computer Science next year when I go to college so I think I'll just further learn what I am learning right now. :P

If I can find someone to sell me two 512mb sticks of ram or one 1gb stick of ram to slip into my pc I would be able to start working on XNA like I want to but I can't. :(

---
May consider though learning C++, take what I learn and maybe make some mods for games, older games but a mod lol.

May consider something for pokemon games, open up the file and finds the players bag, pokemon, pokedex and PC pokemon and then allows modification of it to your liking as well as the stats of the pokemon. I don't think something as easy as that has been made so that would be a fun project for if I learn C++.

The following user thanked Pichu for this useful post:

Chrom3D

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo