Добавление папки в специальные папки C#

Я хочу сохранить файл XML в этот каталог... C:\Users\john\AppData\Roaming\game\data.xml

Я могу перемещаться здесь... string PATH = Environment.SpecialFolder.ApplicationData; Но как мне создать папку с игрой и сохранить data.xml здесь?

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);

Надеюсь, это поможет.

Другие вопросы по тегам