user9964422
user9964422

Reputation: 119

C# visual studio user input for file path and file name

I have the current code below to save a .csv file on the button click event. However, I want the user to be able to choose the file path and document name. Any suggestions on how to go about that? I could read the text from a text box for a file name, but what about the file path with out the user having to type of it in?

        long[][] finalResultArray = dataList.Select(a => a.ToArray()).ToArray();
        string filePath = @"C:\test\test.csv"; 
        int length = finalResultArray.GetLength(0);
        StringBuilder sb = new StringBuilder();
        for(int index = 0; index < length; index++)
        {
            sb.AppendLine(string.Join(",", finalResultArray[index]));
        }
        File.WriteAllText(filePath, sb.ToString());
        MessageBox.Show("Save Complete!");

Upvotes: 0

Views: 3613

Answers (2)

Suresh
Suresh

Reputation: 15

If you want you can also try OpenFileDialog. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.ShowDialog();

Upvotes: 1

trix
trix

Reputation: 995

You could use the SaveFileDialog class for this:

SaveFileDialog saveFileDialog = new SaveFileDialog();

if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
    File.WriteAllText(saveFileDialog.FileName, sb.ToString());
    MessageBox.Show("Save Complete!");
}

Upvotes: 3

Related Questions