c# - Save structure in binary file and then read from it -


My program (in WinForms) is a test of some topics. I have such a structure, where I save my questions for this test I:

  dictionary & lt; int, question & gt; Question = New Dictionary & lt; int, question & gt; (); Public class questions {public question (string q_text, dictionary & lt; string, bull & gt; ans) {text = q_text; Answer = ans; } Public string text {get; Set; } Public Dictionary & lt; String, bool & gt; Answer {receive; Set; }}   

I have your query (absolutely glossary , question & gt; question = new dictionary & lt; int, question & gt; (); ) In the binary file and each time I start the program, it will be read from this. I have never worked with binary files.

You can serial the object and save it in a file. But you have to mark your class with [serializable]

  /// & lt; Summary & gt; /// HD /// and LT; / Summary & gt; Public Static Zero PersistObject () {Logger.Debug ("PersistObject: Start"); // FileStream Stream Stay To File = File Open (_filePath); BinaryFormatter formatter = new BinaryFormatter (); formatter.Serialize (stream, objectToSave); Stream.Close (); The wood Debug ("Persist Object: Ended"); } /// & lt; Summary & gt; Loads the /// object /// & lt; / Summary & gt; Public Static Zero LoadObject () {try.loger ("LoadObject: start"); // Open file to read Deliuger object (file.exis (_pyp path)) {file stream stream = file. OpenRed (_Filepath); BinaryFormatter formatter = new BinaryFormatter (); Object deserializedObject = formatter.Deserialize (stream); Stream.Close (); } wood cutter. Debug ("Load Object: Ended"); } Hold (exception pre) {Logger.Error (ex, ex.Message); }}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -