using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void Serialize(Test obj) { Stream file = File.Open(@c:\tt.dat, FileMode.Create); BinaryFormatter b = new BinaryFormatter(); b.Serialize(file, obj); file.Close(); } public Test DeSerialize(string Name) { Test Obj = new Test(); try { Stream file = File.Open(Name, FileMode.Open); BinaryFormatter b = new BinaryFormatter(); Obj = (Test)b.Deserialize(file); //This is the line which propagate the exception file.Close(); } catch (Exception) { } return Obj; } private void button1_Click(object sender, EventArgs e) { Test o = new Test(); o.a = 44; o.b = 55; Serialize(o); } private void button2_Click(object sender, EventArgs e) { Test o = DeSerialize(@c:\tt.dat); MessageBox.Show(o.a.ToString()); MessageBox.Show(o.b.ToString()); } } } [Serializable()] public struct Test { public double a; public double b; }
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.