System.IO不无法创建游戏存档文件。我尝试运行管理员联系方式,并没有统一。
调试日志:
FileNotFoundException: Could not find file "C:\Users\HP\AppData\LocalLow\NameName\BeautyGame\gamesettings.json"
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool)
和代码:
string jsonData = JsonUtility.ToJson(gameSettings, true);
File.WriteAllText(Application.persistentDataPath + "/gamesettings.json", jsonData));
我建议还是用FileStream
和StreamWriter
并确保FileMode.Create
设置。同时创建文件夹,如果它不存在。
var folderPath = Application.persistentDataPath;
// Create directory if not exists
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
不是使用qazxsw POI创建独立于系统的路径字符串
Path.Combine
现在写的文件
var filePath = Path.Combine(folderPath, "gamesettings.json");
但是,对于本地测试从UnityEditor在PC上我不希望应用到数据泄漏到我的开发PC,而是把它变成某处例如// Create file or overwrite if exists
using (var file = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (var writer = new StreamWriter(file, Encoding.UTF8))
{
writer.Write(content);
}
}
只有在构建使用StreamingAssets
persistentDataPath