user3733200
user3733200

Reputation: 49

Adding a folder in special folders C#

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?

Upvotes: 0

Views: 2585

Answers (1)

Harley Pebley
Harley Pebley

Reputation: 103

// 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.

Upvotes: 1

Related Questions