0

I want to save an XML file into this directory... C:\Users\john\AppData\Roaming\game\data.xml

I can navigate here... string PATH = Environment.SpecialFolder.ApplicationData; But how do I create the game folder and save data.xml here?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user3733200
  • 49
  • 1
  • 7
  • 1
    The `System.IO` namespace should have everything you need. What have you tried? – crthompson Jul 16 '14 at 02:57
  • If you update the code in the other [question](http://stackoverflow.com/questions/24770229/c-sharp-crashes-on-save-doc-savepath) you recently asked to use `Environment.SpecialFolder.ApplicationData` it will very likely just work. – GusP Jul 16 '14 at 03:03

1 Answers1

1
// Place this at the top of the file
using System.IO;
...
// Whatever you want to save.
var contents = "file contents";

// The app roaming path for your game directory.
var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "game");

// Creates the directory if it doesn't exist.
Directory.CreateDirectory(folder);

// The name of the file you want to save your contents in.
var file = Path.Combine(folder, "data.xml");

// Write the contents to the file.
File.WriteAllText(file, contents);

Hope that helps.