⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serializer.cs

📁 game tetris :), this file is a little too big but it s run well, besides, it also has file descripti
💻 CS
字号:
using System;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Windows.Forms;

namespace GATetrisControl
{
	public class Serializer
	{
		public static object GetFromFile(string fileName)
		{
			string dir = Application.UserAppDataPath;
            string path = dir + @"\" + fileName;

			if(!File.Exists(path))
				return null;

			BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);

			try
			{
				object o = bf.Deserialize(fs);
				fs.Close();
				return o;
			}
			catch(Exception ex)
			{
				fs.Close();
				MessageBox.Show("Could not deserialize object due to:\n"+ex.Message);
				return null;
			}
		}

		public static bool SaveToFile(object o, string fileName)
		{
			string dir = Application.UserAppDataPath;
            string path = dir + @"\" + fileName;

            FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write);
			BinaryFormatter bf = new BinaryFormatter();

			try
			{
				bf.Serialize(fs, o);
				fs.Close();
				return true;
			}
			catch(Exception ex)
			{
				fs.Close();
				MessageBox.Show("Could not serialize object due to:\n"+ex.Message);
				return false;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -