コード例 #1
0
        public static ConnectionsStore Deserialize(string FilePath)
        {
            // Declare the hashtable reference.
            ConnectionsStore myConnections = new ConnectionsStore();

            // Open the file containing the data that you want to deserialize.
            FileStream fs = new FileStream(FilePath, FileMode.Open);

            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                myConnections = (ConnectionsStore)formatter.Deserialize(fs);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            }
            finally
            {
                fs.Close();
            }

            // To prove that the table deserialized correctly,
            // display the key/value pairs.
            return(myConnections);
        }
コード例 #2
0
        public static void Serialize(string filepath, ConnectionsStore myConnections)
        {
            // In this case, use a file stream.
            FileStream fs = new FileStream(filepath, FileMode.Create);

            // Construct a BinaryFormatter and use it to serialize the data to the stream.
            BinaryFormatter formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(fs, myConnections);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
            }
            finally
            {
                fs.Close();
            }
        }