0

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?

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Baked Potato
  • 294
  • 1
  • 4
  • 17
  • 1
    You are giving the user control of the path. – H H Jul 20 '12 at 20:52
  • Ya, I know, but is there a way that i can make a default path instead of Desktop? For example, if there is just a default place i want to save the xml document to instead of going to that path every time, i can just press buttonSaveXML_Click and that path will already be there – Baked Potato Jul 20 '12 at 20:57

2 Answers2

2

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.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

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

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

You can see the Documentation.

Elwi
  • 687
  • 1
  • 5
  • 15