  using Microsoft.Win32;
using System;
using System.Windows.Forms;

namespace C_Windows_1
{
    public partial class Save : Form
    {
        public Save()
        {
            InitializeComponent();
        }

        private void Save_Load(object sender, EventArgs e)
        {
            Read();
        }
        private void BtnPath_Click(object sender, EventArgs e)
        {
            //OpenFileDialog1.ShowDialog();
            //path.Text = OpenFileDialog1.FileName;
            FolderBrowserDialog folder = new FolderBrowserDialog();
            folder.ShowDialog();
            path.Text = folder.SelectedPath;
        }
        private void BtnConSql_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();

            OpenForm(form2);
        }


        private void Use_Set_Click(object sender, EventArgs e)
        {
            //if (ChkExcel.Checked && ChkTXT.Checked)
            //{
            //    MessageBox.Show("Excel和TXT不可多选！");
            //}
            //else if (ChkTXT.Checked && path.Text.Contains(".txt") is false && path.Text.Length > 0)
            //{
            //    MessageBox.Show("txt路径错误！");

            //}
            //else if (ChkExcel.Checked && path.Text.Contains(".xls") is false && path.Text.Length > 0)
            //{
            //    MessageBox.Show("excel路径错误!");
            //}
            
            
                //if(ChkMES.Checked)
                //{
                //    //mes的文件夹路径
                //    if (!Directory.Exists(System.Environment.CurrentDirectory + "\\Log"))
                //    {
                //        Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\Log");
                //    }
                //}

                Set();
                Form1.f1.path.Text = path.Text;
                Form1.f1.ChkExcel.Checked = ChkExcel.Checked;
                Form1.f1.ChkSql.Checked = ChkSql.Checked;
                Form1.f1.ChkTXT.Checked = ChkTXT.Checked;
                Form1.f1.ChkMES.Checked = ChkMES.Checked;
                Form1.f1.ChkCSV.Checked = ChkCSV.Checked;
                this.Close();
            
        }

        private void BtnConMES_Click(object sender, EventArgs e)
        {
            MESConfig me = new MESConfig();
            OpenForm(me);
        }


        //写入
        private void Set()
        {

            RegistryKey regName;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true);

            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set");
            }

            regName.SetValue("excel", ChkExcel.Checked.ToString());
            regName.SetValue("txt", ChkTXT.Checked.ToString());
            regName.SetValue("sql", ChkSql.Checked.ToString());
            regName.SetValue("mes", ChkMES.Checked.ToString());
            regName.SetValue("csv", ChkCSV.Checked.ToString());
            regName.SetValue("path", path.Text);

            regName.Close();
        }


        //读出
        private void Read()
        {

            RegistryKey regName;

            regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true);

            if (regName is null)
            {
                regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set");
            }

            regName.OpenSubKey("User");
            if (regName.GetValue("excel") is null)
            {
                ChkExcel.Checked = false;
            }
            else
            {
                ChkExcel.Checked = Convert.ToBoolean(regName.GetValue("excel").ToString());

            }

            if (regName.GetValue("txt") is null)
            {
                ChkTXT.Checked = false;
            }
            else
            {
                ChkTXT.Checked = Convert.ToBoolean(regName.GetValue("txt").ToString());

            }

            if (regName.GetValue("sql") is null)
            {
                ChkSql.Checked = false;
            }
            else
            {
                ChkSql.Checked = Convert.ToBoolean(regName.GetValue("sql").ToString());

            }
            if (regName.GetValue("mes") is null)
            {
                ChkMES.Checked = false;
            }
            else
            {
                ChkMES.Checked = Convert.ToBoolean(regName.GetValue("mes").ToString());

            }
            if (regName.GetValue("csv") is null)
            {
                ChkCSV.Checked = false;
            }
            else
            {
                ChkCSV.Checked = Convert.ToBoolean(regName.GetValue("csv").ToString());

            }
            if (regName.GetValue("path") is null)
            {
                path.Text = "";
            }
            else
            {
                path.Text = regName.GetValue("path").ToString();
                regName.Close();
            }
        }




        public void OpenForm(System.Windows.Forms.Form frm)
        {
            if (frm == null) return;
            foreach (System.Windows.Forms.Form f in System.Windows.Forms.Application.OpenForms)
            {
                if (f.Name == frm.Name)
                {
                    f.Activate();
                    f.Show();
                    frm.Dispose();
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                    return;
                }
            }
            frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            frm.Show();
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
        }


    }
}
