【问题标题】:Adding a folder in special folders C#在特殊文件夹中添加文件夹 C#
【发布时间】:2023-04-01 09:42:01
【问题描述】:

我想将一个 XML 文件保存到此目录中... C:\Users\john\AppData\Roaming\game\data.xml

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

【问题讨论】:

  • System.IO 命名空间应该有你需要的一切。你试过什么?
  • 如果您更新另一个question 中的代码,您最近要求使用Environment.SpecialFolder.ApplicationData,它很可能会正常工作。

标签: c# path directory special-folders


【解决方案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);

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多