序列化

来源:互联网 发布:unity3d 暴风魔镜 编辑:程序博客网 时间:2024/05/03 18:47

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace N_server
{
    class AppClass
    {
        /// <summary>
        /// 服务器端口
        /// </summary>
        public static string ServerIP = "127.0.0.1";
        public static string SQLuser = "";
        public static string SQLpass = "";
        public static string SQLdata = "";

        public static string ServerIP2 = "";
        public static string ComName = "";
        public static string ComPort = "";
        public static string DoubleIP = "";
        public static string DoublePort = "";

        public static string Conn = "";
        /// <summary>
        /// 保存服务器设置
        /// </summary>
        public static void SaveSetting()
        {
            StreamWriter sw = new StreamWriter("Config.Dat", false);
            sw.WriteLine(ServerIP);
            sw.WriteLine(SQLuser);
            sw.WriteLine(SQLpass);
            sw.WriteLine(SQLdata);

            sw.WriteLine(ServerIP2);
            sw.WriteLine(ComName);
            sw.WriteLine(ComPort);
            sw.WriteLine(DoubleIP);
            sw.WriteLine(DoublePort);
            sw.Close();
        }
        /// <summary>
        /// 加载服务器设置
        /// </summary>
        public static void LoadSetting()
        {
            StreamReader sr = new StreamReader("Config.Dat");
            AppClass.ServerIP = sr.ReadLine();
            AppClass.SQLuser = sr.ReadLine();
            AppClass.SQLpass = sr.ReadLine();
            AppClass.SQLdata = sr.ReadLine();

            AppClass.ServerIP2 = sr.ReadLine();
            AppClass.ComName = sr.ReadLine();
            AppClass.ComPort = sr.ReadLine();
            AppClass.DoubleIP = sr.ReadLine();
            AppClass.DoublePort = sr.ReadLine();
            sr.Close();
            Conn = "server=" + AppClass.ServerIP + ";uid=" + AppClass.SQLuser + ";pwd=" + AppClass.SQLpass + ";database=" + AppClass.SQLdata + "";
        }

        /// <summary>
        /// xml序列化DataTable
        /// </summary>
        public static string dataTostr(DataTable thedata)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);
            XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
            serializer.Serialize(writer, thedata);
            writer.Close();
            return sb.ToString();
        }

        /// <summary>
        /// xml反序列化DataTable
        /// </summary>
        public static DataTable strTodata(string Dstr)
        {
            StringReader strReader = new StringReader(Dstr);
            XmlReader xmlReader = XmlReader.Create(strReader);
            XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
            DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
            return dt;
        }

        /// <summary>
        /// 把对象序列化并返回相应的字节
        /// </summary>
        /// <param name="pObj">需要序列化的对象</param>
        /// <returns>byte[]</returns>
        public static byte[] SerializeObject(object pObj)
        {
            if (pObj == null)
                return null;
            System.IO.MemoryStream _memory = new System.IO.MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(_memory, pObj);
            _memory.Position = 0;
            byte[] read = new byte[_memory.Length];
            _memory.Read(read, 0, read.Length);
            _memory.Close();
            return read;
        }
        /// <summary>
        /// 把字节反序列化成相应的对象
        /// </summary>
        /// <param name="pBytes">字节流</param>
        /// <returns>object</returns>
        public static object DeserializeObject(byte[] pBytes)
        {
            object _newOjb = null;
            if (pBytes == null)
                return _newOjb;
            System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
            _memory.Position = 0;
            BinaryFormatter formatter = new BinaryFormatter();
            _newOjb = formatter.Deserialize(_memory);
            _memory.Close();
            return _newOjb;
        }

        public static object toObject(string theValue)      //二进制到64数字的字符
        {
            BinaryFormatter bf = new BinaryFormatter();
            byte[] byteValue = Convert.FromBase64String(theValue);
            MemoryStream mem = new MemoryStream(byteValue);
            object ObjectValue = bf.Deserialize(mem);
            mem.Close();
            return ObjectValue;
        }

        public static string toString(object theValue)      //二进制到64数字的字符
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream mem = new MemoryStream();
            bf.Serialize(mem, theValue);
            string returnString = Convert.ToBase64String(mem.ToArray());
            mem.Close();
            return returnString;
        }

        public static void WriteFile(string txt)
        {
            string datatxt = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
            StreamWriter sw;
            if (File.Exists("LOG/" + datatxt + ".txt") == true)
            {
                sw = File.AppendText("LOG/" + datatxt + ".txt");
            }
            else
            {
                sw = File.CreateText("LOG/" + datatxt + ".txt");
            }
            sw.WriteLine(DateTime.Now + "     " + txt);
            sw.Close();
            sw.Dispose();
        }

        public static string WriteImage(Image Objecttxt)
        {
            string FileName = suiji() + ".gif";
            Objecttxt.Save(@"Img/" + FileName, System.Drawing.Imaging.ImageFormat.Gif);
            return FileName;
        }

        public static string suiji()
        {
            string Numtxt = "";
            int num = 0;
            // for (int i = 1; i < 5; i++)
            // {
            Random r = new Random(); //实例化一个随机数对象;
            num = r.Next(1000, 9999); //用这个对象产生0到4之间的随机数,也就是你定义的5个数组的下标;

            Numtxt = num.ToString();
            // }
            return Numtxt.ToString();
        }

    }
}

原创粉丝点击