Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Music Manager 1.1

Rate me:
Please Sign up or sign in to vote.
4.85/5 (36 votes)
15 Mar 2008CPOL6 min read 146.2K   7.7K   149  
Music library, Probability based random play
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace MusicManager
{
	/// <summary>
	/// Summary description for SerializeHelper.
	/// </summary>
	public class SerializeHelper
	{
		private static SerializeHelper helper;
		public SerializeHelper()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		public static SerializeHelper GetHelper()
		{
			if(helper==null)
				helper=new SerializeHelper();
			return helper;
		}
        public void SaveFile(object obj, string FileName)
        {
            FileStream fs = new FileStream(FileName, FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(fs, obj);
            fs.Close();
        }
        public object ReadFile(string FileName)
        {
            FileStream fs = new FileStream(FileName, FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();
            object tempobjectdata = (object)bformatter.Deserialize(fs);
            fs.Close();
            return tempobjectdata;
        }
		public string Serialize(object obj)
		{
			try
			{
				MemoryStream stream=new MemoryStream();
				BinaryFormatter bformatter = new BinaryFormatter();
				bformatter.Serialize(stream, obj);
				byte [] bArray;
				bArray=stream.ToArray();
				stream.Position=0;
				string tempobjectdatastring=Convert.ToBase64String(bArray);
				stream.Close();
				return tempobjectdatastring;
			}
			catch(Exception e)
			{
				throw e;
			}
		}
		public object DeSerialize(string tempobjectdatastring)
		{
			try
			{
				byte [] bArray;	
				bArray=Convert.FromBase64String(tempobjectdatastring);
				MemoryStream stream=new MemoryStream();
				stream.Position=0;
				stream.Write(bArray,0,bArray.Length);
				stream.Position=0;
				BinaryFormatter bformatter = new BinaryFormatter();
				object tempobjectdata = (object)bformatter.Deserialize(stream);
				stream.Close();
				return tempobjectdata;
			}
			catch(Exception e)
			{
				throw e;
			}
		}
		public object MakeClone(object obj)
		{
			try
			{
				MemoryStream stream=new MemoryStream();
				BinaryFormatter bformatter = new BinaryFormatter();
				bformatter.Serialize(stream, obj);
				stream.Position=0;
				object tempobjectdata = (object)bformatter.Deserialize(stream);
				return tempobjectdata;
			}
			catch(Exception e)
			{
				throw e;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions