C# Code Snippet - Save object to file

C# Code Snippet - Save object to file

C# Code Snippet - Save object to file

(C-Sharp) C# code snippet save object to external file. This function can be use to serialize objects into byte array and save them in external file.

Bookmark:

C# Code Snippet - Save object to file

This .Net C# code snippet save object to external file. This function can be use to serialize objects into byte array and save them in external file. This function uses System.IO and System.Runtime.Serialization.Formatters.Binary name spaces to save object to file. This function useful to implement database cache control system for high traffic databases by storing cached query object data in external files and read them back to database objects. Modify the exception handling section as to your project requirements.

/// <summary>
/// Function to save object to external file
/// </summary>
/// <param name="_Object">object to save</param>
/// <param name="_FileName">File name to save object</param>
/// <returns>Return true if object save successfully, if not return false</returns>
public bool ObjectToFile(object _Object, string _FileName)
{
    try
    {
        // create new memory stream
        System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();

        // create new BinaryFormatter
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                    = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        // Serializes an object, or graph of connected objects, to the given stream.
        _BinaryFormatter.Serialize(_MemoryStream, _Object);

        // convert stream to byte array
        byte[] _ByteArray = _MemoryStream.ToArray();

        // Open file for writing
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        // Writes a block of bytes to this stream using data from a byte array.
        _FileStream.Write(_ByteArray.ToArray(), 0, _ByteArray.Length);

        // close file stream
        _FileStream.Close();

        // cleanup
        _MemoryStream.Close();
        _MemoryStream.Dispose();
        _MemoryStream = null;
        _ByteArray = null;

        return true;
    }
    catch (Exception _Exception)
    {
        // Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }

    // Error occured, return null
    return false;
}


Here is a simple example showing how to use above function (ObjectToFile). This example illustrates how to save picturebox image object to external file.

ObjectToFile(pictureBox1.Image, "c:\\image-object.dat");


C# Keywords Used:

  • FileStream
  • FileMode
  • FileAccess
  • MemoryStream
  • BinaryFormatter
  • Serialize
  • byte
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, C#, CLI, Byte Array, Object to file, FileStream, FileMode, BinaryFormatter, Serialize, Cache control, Save object to file
  • Programming Language : C# (C-Sharp)

External Resources:

Frederick :: March 08-2010 :: 12:25 PM

i have a problem with a program i am trying to do. i would like do a file extention of this program so that when i put the information in this program i open the information with it not with the wordpad (.txt) but i dont know how to do can someone pls give me a tip.

Leave a comment