I wrote an article about resuming the download from a breakpoint before, address: https://blog.csdn.net/yzj_xiaoyue/article/details/80023655, and then I want to record the downloaded content in the file, if the network is interrupted In the case of json.net, you can continue to download according to the configuration file. At the prompt of the manager, the Json data exists in the file, and the data is serialized and stored by understanding Newtonsoft.Json of json.net.
After integration and modification, I wrote a Json helper class myself, and put the code directly:
using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DownloadTaskDemo { class JsonHelper { /// <summary> /// Get the content in the json file through the file path /// </summary> /// <param name="cfgFilePath">Configuration file storage path</param> /// <returns></returns> public static ConfigureModel ReadJson(string cfgFilePath) { ConfigureModel cfgModel = new ConfigureModel(); //Serialization string cfgSerializer = JsonConvert.SerializeObject(cfgModel); // deserialize ConfigureModel debc1 = JsonConvert.DeserializeObject<ConfigureModel>(cfgSerializer); if (!File.Exists(cfgFilePath)) { File.Create(cfgFilePath); } //read json file using (StreamReader sr = new StreamReader(cfgFilePath)) { try { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new JavaScriptDateTimeConverter()); serializer.NullValueHandling = NullValueHandling.Ignore; //Build the read stream of Json.net JsonReader reader = new JsonTextReader(sr); //Deserialize the read Json.net reader stream and load it into the model cfgModel = serializer.Deserialize<ConfigureModel>(reader); return cfgModel; } catch (Exception ex) { ex.Message.ToString(); } } return null; } /// <summary> /// Write Json data to Json file /// </summary> /// <param name="cfgFilePath">Path to configuration file</param> /// <param name="cfgModel">Configuration class object</param> /// <returns></returns> public static bool WriteJson(string cfgFilePath, ConfigureModel cfgModel) { //Check if the file exists if (!File.Exists(cfgFilePath)) { File.Create(cfgFilePath); } // write model data to file using (StreamWriter sw = new StreamWriter(cfgFilePath)) { try { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new JavaScriptDateTimeConverter()); serializer.NullValueHandling = NullValueHandling.Ignore; //Build the write stream of Json.net JsonWriter writer = new JsonTextWriter(sw); //Serialize the model data and write it to the JsonWriter stream of Json.net serializer.Serialize(writer, cfgModel); //ser.Serialize(writer, ht); writer.Close(); sw.Close(); return true; } catch (Exception ex) { ex.Message.ToString(); } } return false; } } }
A custom class is used in the above code as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DownloadTaskDemo { class ConfigureModel { //file name public string FileName { get; set; } //MD5 public string MD5 { get; set; } //file source public string FileUrl { get; set; } // downloaded content public long Position { get; set; } // total file size public long FileLength { get; set; } // Whether the download is complete public bool IsDownload { get; set; } // file creation time public DateTime CreateDate { get; set; } } }
I hope that I can use it for future reference, and I hope to help more people in need, thank you.