在特殊文件夹中添加文件夹C#

时间:2014-07-16 02:52:40

标签: c# path directory special-folders

我想将XML文件保存到此目录中... C:\用户\约翰\应用程序数据\漫游\游戏\ data.xml中

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

1 个答案:

答案 0 :(得分: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);

希望有所帮助。