我无法从文件系统读取json配置文件。我正在将对象序列化为这样的文件:
string json = JsonConvert.SerializeObject(Settings.Instance);
Directory.CreateDirectory(configFileDir);
File.WriteAllText(configFileDir + configFilePath, json);
完整路径为
C:\Users\username\Documents\MyApp\config.json
现在我可以用以下几行来阅读它:
string json = File.ReadAllText(configFileDir + configFilePath);
Instance = JsonConvert.DeserializeObject<Settings>(json);
我的问题是,它表明在我构建了新版本的程序后,它无法正确读取某些属性。重新启动时,所有设置都可以正常加载,但是当我重新构建时,该文件将不会加载更长的字符串值(令牌)。
打印json字符串显示某些键的空字符串值,当在记事本中打开文件时,这些键显然具有值。
有人猜吗?
问候, 迈克
编辑:
json看起来像这样:
{
"DisplayLanguage": "en-US",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjM4OWZmMTIxZWU1MmI1ZDk3NWEzY2E0NzYwN2JiZTIzODc1ZjViMDUxNzY1ZDE1MGExZDgyNTM0ODUyMjE4MTcxOGQ0ZTA2NTZlM2JhYzg5In0.eyJhdWQiOiJmNWIwYjcwMS01ODEzLTQ5OGMtYmE1NS1kMWZhOGM5YzQxMGUiLCJqdGkiOiIzODlmZjEyMWVlNTJiNWQ5NzVhM2NhNDc2MDdiYmUyMzg3NWY1YjA1MTc2NWQxNTBhMWQ4MjUzNDg1MjIxODE3MThkNGUwNjU2ZTNiYWM4OSIsImlhdCI6MTUzNzI4NDA0MCwibmJmIjoxNTM3Mjg0MDQwLCJleHAiOjE1Njg4MjAwNDAsInN1YiI6ImE1MTk2OTNjLTAzMGMtNDdjMi04MGI5LTZiNTc5YmQ3MzZlNSIsInNjb3BlcyI6W119.yjzApeU54uXch-3V-x82V-mXfmyr-HG0LsxQMG74nrJP_dBmSi8MuB5mD3Zc07qRnmKnyep7YGYVdhvmukALIY4u5lXrVGXFx_U_EWMyEKce9BxMrM9qbqy-oZ5hfJ9rogUsKeQakkpnD4yciTqn0h8sg-uo6H0-pcWIubhWA9OYmU5-vAIhVRJrQBggW305auCiyKVYcoRQowf12RJhrudn8F_eOhEbrV4A9PZVHTIIVRRFFTCfcHNYDsW9cS1RJ07sOE_rg8-yjQt74gmClgZguYngjQiukCsJtzMBRywRUT24DPGauPsAyYHWzitfe-KaGBbUSLFr2YOK3cDhZI7POv6QaFH-iIcw2-2iTR_h2_rk_iVYQNsyv0lU9KglrsUjiAonqESyHvAyYcSVDaqRobDdG9iXSpKw4jDSL7vkt7_IkMQ1cCftvqueglDNosygR6zbmi4L7ESW5eia8n_6-q6tkSaPageupXXmEBMBZNQF_p8HQf9J8ma7hxc-bcPGFkHFFBhrWlTJg62fT9AR9wIq7zueYpamxkGgfZnXF9QMzEjXNobs3paF9pouTv7H6v1iHJ_90WpGe3NVnJTHxPWVImtb9npvfPgWcYE8Trz6xoTbE81Al5fLjrAK7antgS0DkYkzMp1rxSUQxw_qwTLywuFJJLcwhY5mY2s",
"GUID": "993bc5f2-0b2b-48da-8fc7-94a7b96dc322",
"sortedByRating": true,
"lists": ""
}
只有令牌和GUID似乎会丢失其值,DisplayLanguage和其他加载都很好。 我用log4net打印json,这不是问题。 我得到的文件路径为:
private static readonly string configFileDir = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + @"\MyApp";
private static readonly string configFilePath = @"\config.json";
设置类如下:
public class Settings
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static readonly string configFileDir = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + @"\MyApp";
private static readonly string configFilePath = @"\config.json";
private static Settings _instance;
public static Settings Instance
{
get { return _instance ?? (_instance = new Settings()); }
private set { _instance = value; }
}
[JsonIgnore]
public static readonly string AddInVersion = "1.4.0.3";
[JsonProperty("DisplayLanguage")]
public static string DisplayLanguage { get; set; } = "";
[JsonProperty("token")]
public static string AuthToken { get; set; } = "";
[JsonProperty("GUID")]
public static string GUID { get; set; } = "";
[JsonProperty("sortedByRating")]
public static bool RecommendationsSortedByRating { get; set; } = true;
[JsonProperty("DistLists")]
public static string DistLists { get; set; } = "";
public static void Load()
{
try
{
log.Info("trying to read config file: " + configFileDir + configFilePath);
string json = File.ReadAllText(configFileDir + configFilePath);
log.Info("read: " + json);
Instance = JsonConvert.DeserializeObject<Settings>(json);
log.Info("Config file loaded!");
log.Debug(JsonConvert.SerializeObject(Instance, Formatting.Indented));
}
catch (Exception)
{
log.Info("Config file not found or could not be loaded. Using Default Settings");
}
}
public static void Save(string settingToSave = "")
{
try
{
string json = JsonConvert.SerializeObject(Settings.Instance, Formatting.Indented);
Directory.CreateDirectory(configFileDir);
File.WriteAllText(configFileDir + configFilePath, json);
log.Info("Config file successfully saved! (" + settingToSave + ")");
}
catch (Exception)
{
log.Error("Config file could not be saved!");
}
}
答案 0 :(得分:-1)
您正在很好地阅读文件不是路径问题。 检查设置类
public class Settings{
public string DisplayLanguage{ get; set; }
public string token{ get; set; }
}
如果您错过任何一个,可能会造成问题。还检查格式。 Log4net可能不显示长字符串。尝试检查类中标记的大写句子大小写。