Posted By: Emyr | May 10th @ 6:16 AM
page 1 of 1
Comments: 9 | Views: 1313
Emyr
Emyr
In front of my computer....

Hello all,

I'm new here so please bear with me.  I've been writing a program that I need to store the contents of a text box in a file.  The code is pasted here.  I'm not sure what I'm doing wrong.  (I've only been doing c# for 3 months....)

Thanks

Emyr

===== [ Code snippet ] =====


        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();           
            save.Filter = "Expression Files(*.exp)|*.exp";

            try
            {
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string y;
                    y = textBox1.Text;
                    //MessageBox.Show(y);
                    FileStream aFile = new FileStream(save.FileName, FileMode.CreateNew);
                    sw = new StreamWriter(save.FileName);
                    //MessageBox.Show("File Saved");
                    sw.WriteLine(y);
                    //saveFile(save.FileName);

                }
            }
            catch (ArgumentException)
            {
                // Do nothing.  User canceled the operation
            }
            catch (IOException)
            {
                MessageBox.Show("There was an error in writing the file");
            }

TommyCarlier
TommyCarlier
Trust me, I'm from the Internets
If you create a FileStream, the file will be locked, so StreamWriter will not be able to write to it. I suggest you also learn about the IDisposable-interface and the using-pattern. Anyway, there's actually a simpler solution for this problem: the static File-class has some useful methods for dealing with files, including a method WriteAllText, which writes text to a file.
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
    using(SaveFileDialog save = new SaveFileDialog())
    {
        save.Filter = "Expression Files (*.exp)|*.exp";
        if (save.ShowDialog(this) == DialogResult.OK)
            try
            {
                File.WriteAllText(save.FileName, textBox1.Text);
            }
            catch(IOException ex)
            {
                MessageBox.Show(this
                    , "Could not save the expression: " + ex.Message
                    , "Error while writing file"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Error);
            }
    }
}
evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
Avoid using a filestream if you don't have to:

Given two textboxes called file (which stores filename) and some textbox texty with the contents)


System.IO.File.WriteAllText(file.Text, texty.Text)


or if you're filename is stored in some variable (say, string filename)

System.IO.File.WriteAllText(filename, texty.Text)


it's much briefer and therefore less prone to errors.
wisemx
wisemx
Live it
We 9ers write the best code, not the safest. Big Smile
CannotResolveSymbol
CannotResolveSymbol
{insert caption here}
wisemx wrote:
We 9ers write the best code, not the safest.


Why is writing 5-6 lines of code using a FileStream "better" than writing one line of code with WriteAllText?  The library's there for you to use, why not use it?
littleguru
littleguru
allein, allein,... allein, allein!
I like this combination here... Especially since the FileStream is never used after it's creation... awesome Wink

FileStream aFile = new FileStream(save.FileName, FileMode.CreateNew);
sw = new StreamWriter(save.FileName);

If you don't love the WriteAllText method you can do it on your own:

string text = "foobar love";

using (StreamWriter writer = File.CreateText(fileName))
{
    writer.Write(text);
}
evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
wisemx wrote:
We 9ers write the best code, not the safest.


No offense, but I doubt that, although given that you have only recommended the FileStream class but provided no actual code it's difficult to say.