tags:

views:

7537

answers:

4

This is a winforms application.

In windows, I want the user to click and button, and then a popup should make the user select the path of where they want to save the file.

A: 

StringBuilder.ToString() can be passed to the TextStream.Write() method after creating the file.

Using the SaveFileDialog class, you can let the user select path and file name - in a standard way. Detailed examples in the doc.

gimel
The OpenFileDialog will, by default, require the file to exist. The SaveFileDialog class is the way to go.
Jon Grant
the content of the file to be saved is in the stringbuilder
Anonymous123
+10  A: 

You want the WriteAllText function.

using (SaveFileDialog dialog = new SaveFileDialog()) {
    if (dialog.ShowDialog(this) == DialogResult.OK) {
        File.WriteAllText(dialog.FileName, yourStringBuilder.ToString());
    }
}
Jon Grant
+1  A: 

StringBuilder.ToString() will get you the string.

This link will show you how to write text to a file.

This link will show you how to call SaveFileDialog and pass a stream to it to save.

Hope that helps.

itsmatt
A: 

Think no longer...

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;

namespace WindowsFormsApplication2 {
public partial class Form1 : Form {

    StringBuilder sb = new StringBuilder();

    public Form1() {
        InitializeComponent();

        sb.Append("This is going ");
        sb.Append("to be saved to a text file");
    }

    private void button1_Click(object sender, EventArgs e) {
        using (SaveFileDialog dlg = new SaveFileDialog()) {
            if (dlg.ShowDialog() == DialogResult.OK) {
                string fileName = dlg.FileName;
                SaveToFile(fileName);
            }
        }
    }

    private void SaveToFile(string fileName) {
        System.IO.TextWriter w = new System.IO.StreamWriter(fileName);
        w.Write(sb.ToString());
        w.Flush();
        w.Close();
    }
}
sbeskur
Why mess about with StreamWriters when there is a built-in function to do it for you?
Jon Grant
Hey, Pretty cool...I've not seen that function. I have learned something new today:D
sbeskur