C#

c# - 윈도우 위치 기억. 파일에 저장 로드

안녕1999 2020. 6. 20. 22:41
	enum SAVEDATA {SD_WIN_left,SD_WIN_top,
            SD_MAX };
        Int32[] m_save_data = new Int32[(Int32)SAVEDATA.SD_MAX];
        const string m_file = "data.sav";
        long GetFileSize(string file)
        {
            try
            {
                FileInfo fi = new System.IO.FileInfo(file);
                return fi.Length;
            }
            catch (Exception e)
            {
                return 0;
            }
        }
        void LoadData()
        {
            long size=GetFileSize(m_file);
            if (size > 0)
            {
                using (BinaryReader f = new BinaryReader(File.Open(m_file, FileMode.Open)))
                {
                    byte[] bytes = new byte[m_save_data.Length * sizeof(Int32)];
                    f.Read(bytes, 0, bytes.Length);
                    Buffer.BlockCopy(bytes, 0,  m_save_data,0, bytes.Length);
                    f.Close();
                }
            }
        }
        void SaveData()
        {
            using (BinaryWriter f = new BinaryWriter(File.Open(m_file, FileMode.Create)))
            {
                byte[] bytes = new byte[m_save_data.Length * sizeof(int)];
                Buffer.BlockCopy(m_save_data, 0, bytes, 0, bytes.Length);
                f.Write(bytes, 0, bytes.Length);
                f.Close();
            }
        }
	private void Form1_Load(object sender, EventArgs e)
        {
            LoadData();

            StartPosition = FormStartPosition.Manual;
            SetDesktopLocation(m_save_data[(int)SAVEDATA.SD_WIN_left], m_save_data[(int)SAVEDATA.SD_WIN_top]);
        }	

	private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_save_data[(int)SAVEDATA.SD_WIN_left] = Location.X;
            m_save_data[(int)SAVEDATA.SD_WIN_top]= Location.Y;
            SaveData();
        }