在c#的特殊文件夹中添加文件夹

本文关键字:文件夹 添加 | 更新日期: 2023-09-27 18:13:12

我想保存一个XML文件到这个目录…C:'Users'john'AppData'Roaming'game' data.xml

我可以在这里导航…string PATH = Environment.SpecialFolder.ApplicationData;但是我如何在这里创建游戏文件夹并保存data.xml ?

在c#的特殊文件夹中添加文件夹

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

希望对你有帮助。