Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在特殊文件夹C中添加文件夹#_C#_Path_Directory_Special Folders - Fatal编程技术网

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

C# 在特殊文件夹C中添加文件夹#,c#,path,directory,special-folders,C#,Path,Directory,Special Folders,我想将XML文件保存到此目录中。。。 C:\Users\john\AppData\Roaming\game\data.xml 我可以在这里导航。。。字符串路径=Environment.SpecialFolder.ApplicationData; 但是如何创建游戏文件夹并在此处保存data.xml // Place this at the top of the file using System.IO; ... // Whatever you want to save. var contents

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

我可以在这里导航。。。字符串路径=Environment.SpecialFolder.ApplicationData; 但是如何创建游戏文件夹并在此处保存data.xml

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

希望这能有所帮助。

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