SoFunction
Updated on 2025-03-07

c# save window position size operation class (serialization and file read and write functions)


namespace
{
    public class Setting
    {
        ///<summary>
/// Serialize the object into a byte array
        ///</summary>
        public static byte[] SerializeObject(object obj)
        {
            if (obj == null)
                return null;
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            (ms, obj);
            = 0;
            byte[] bytes = new byte[];
            (bytes, 0, );
            ();
            return bytes;
        }

        ///<summary>
/// Deserialize the byte array into an object
        ///</summary>
        public static object DeserializeObject(byte[] bytes)
        {
            object obj = null;
            if (bytes == null)
                return obj;
            MemoryStream ms = new MemoryStream(bytes);
            = 0;
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                obj = (ms);
            }
            catch { obj = null; }
            ();
            return obj;
        }

        public static bool Save(string path, object value, bool isCeranew)
        {
//If there is no creation file
            FileStream fs;
            if ((!(path)) && isCeranew)
            {
                try
                {
                    fs = (path);
                }
                catch
                {
                    return false;
                }
            }
//Open if it exists
            else
            {
                try
                {
                    fs = (path, , );
                }
                catch
                {
                    return false;
                }

            }
//Write a file
            byte[] buffer = SerializeObject(value);

            try
            {
                for (long i = 0; i < ; i++)
                    (buffer[i]);
            }
            catch
            {
                return false;
            }
            ();
            return true;
        }

        public static object Read(string path)
        {
            FileStream fs;
            try
            {
                fs = (path);
            }
            catch
            {
                return null;
            }

//Read into cache
            StreamReader sreader = new StreamReader(fs);
            string str = ();
            ();
            ();
//Analysis content
            byte[] buffer = (str);
            return DeserializeObject(buffer);
        }
        [Serializable]
        public struct FormSizeandLocation
        {
            public int SizeW;
            public int SizeH;
            public int LocationX;
            public int LocationY;
            public int Style;
        }
      private static  fsp = new ();
        public static void AddRenewFormSizeControl(Form form)
        {
            += new FormClosingEventHandler(FormcloseEvent);
            += new EventHandler(FormloadEvent);
         }
        private static void FormcloseEvent(object sender, EventArgs e)
        {
            Form form = (Form)sender;
            switch ()
            {
                case :
                    = 2;

                    = ;
                    = ;
                    = ;
                    = ;
                    break;
                case :
                    = 1;
                    break;
                case :
                    = 0;

                    = ;
                    = ;
                    = ;
                    = ;
                    break;
            }
            (() + @"\" + "", fsp, true);
        }
        private static void FormloadEvent(object sender, EventArgs e)
        {
            Form form = (Form)sender;
            object result = (() + @"\" + "");
            if (result != null)
            {
                fsp = ()result;
                switch ()
                {
                    case 2:
                        = ;
                        break;
                    default:
                        = ;
                        break;
                }
                = ;
                = ;
                = new Size(, );

            }
        }
    }
}