GodLess
GodLess

Reputation: 58

C# Multiline textBox to .TXT file

I'm definitely using the wrong method. Actually I'm trying to convert from JAVA to C# and it's beginning to become tough ..

Anyway, I have a textBox1 that is multiline, I write to it by for looping an ArrayList.

The textbox1 looks like this:

Website: https://google.dk
Firmanavn: Google LLC
Email: [email protected]
CVR: 123456
gScore: 1
gLink: googlePageSpeedLink

The code that I use right now, which manages to create a file, but it ends up empty. I am surely doing something wrong, and I'm unsure how to write the textBox to the file.

private void button3_Click(object sender, EventArgs e)
    {
        Stream myStream;
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.Filter = "txt files (*.txt)|";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;


        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            String path = Path.GetFullPath(saveFileDialog1.FileName);

            path = DialogResult.ToString();
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            { 

                File.WriteAllText(path, textBox1.Text);

                myStream.Close();
            }
        }
    }

Any help appreciated... :3 :3 I searched for around 1½ hours on StackOverFlow but I didn't manage to see a sample that would match my way of code :3

I hope it's not a duplicate question, thanks a lot for your answers :)

Upvotes: 0

Views: 78

Answers (1)

Tobias Tengler
Tobias Tengler

Reputation: 7454

You don't need the Stream:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    string path = Path.GetFullPath(saveFileDialog1.FileName);

    File.WriteAllText(path, textBox1.Text);
}

Upvotes: 2

Related Questions