Robin
Robin

Reputation: 2723

how to auto enter filename at the saveFileDialog

I am making a program where the user is asked to enter a world name and a author name. When he clicks finish a saveFileDialog is opened and asks the user to choose a save location. But I want it to automatically enter the name of the world the user entered as file name. But leave the user free to alter it. Is this possible and how can I do it.

Here is my save code, the saveFileDialog is on JSON filter setting.

//Saving the project
if (saveWork.ShowDialog() == DialogResult.OK)
{
string output = JsonConvert.SerializeObject(MainForm.CurrentWorld);
try
{
string name = saveWork.FileName;
using (System.IO.StreamWriter sw = new StreamWriter(name))
sw.WriteLine(output);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Upvotes: 0

Views: 2599

Answers (2)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13794

 SaveFileDialog saveFileDialog = new SaveFileDialog();
 saveFileDialog.InitialDirectory = @"yourDir "; 
 saveFileDialog.FileName = "yourFileName";

Upvotes: 1

SLaks
SLaks

Reputation: 888187

Just set the FileName property before showing the dialog.

Upvotes: 5

Related Questions