Baked Potato
Baked Potato

Reputation: 294

How to make a selected path to save an XML file?

I have a function that can save a .xml file.

private void buttonSaveXML_Click(object sender, EventArgs e)
{
     SaveFileDialog saveFile = new SaveFileDialog();
     saveFile.Filter = "XML Files|*.xml";
     saveFile.Title = "Save a Xml File";
     saveFile.ShowDialog();
     if (saveFile.FileName != "")
     {
         FileStream fs = (FileStream)saveFile.OpenFile();
         dsVersions.WriteXml(fs);
     }
}

What do I add to make a specific path that I want to save it to?

Upvotes: 0

Views: 2968

Answers (2)

Elwi
Elwi

Reputation: 697

Use the property InitialDirectory of SaveFileDialog form. For Example add this to your code:

saveFile.InitialDirectory = "C:\\MyXMLs\\";

You can see the Documentation.

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

You can control the InitialDirectory so that the user will be "in the right place", but you cannot prevent them from switching directories with SaveFileDialog.

That way, they will be in your default path rather than e.g. on the Desktop.

saveFile.InitialDirectory = @"C:\My\Path" ;

Typically I will save the last directory that the user selected to save files to in application configuration and use the user's last directory as the InitialDirectory.

Upvotes: 2

Related Questions