WPF手書きコードプロファイル——一例

13862 ワード

  public class SettingHelper

    {

        //WPF 

        public static readonly string SettingFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\Setting.ini";

        // 

        private SettingHelper()

        {



        }

        // 

        private static class Instance

        {

            internal static readonly SettingHelper Setting = new SettingHelper();

        }



      



        public static SettingHelper GetInstance()

        {

            return Instance.Setting;

        }



        #region API 



        [DllImport("kernel32")]// 0 , 0 

        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);



        [DllImport("kernel32")]// 

        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);



        #endregion





        #region  Ini 



        public string ReadIniData(string section, string key)

        {

            return ReadIniData(section, key, string.Empty);

        }



        public string ReadIniData(string section, string key, string defaultValue)

        {

            if (File.Exists(SettingFilePath))

            {

                StringBuilder s = new StringBuilder(1024);



                GetPrivateProfileString(section, key, defaultValue, s, 1024, SettingFilePath);



                return s.ToString();

            }

            else

            {

                return String.Empty;

            }

        }



        #endregion



        // Ini 

        public bool WriteIniData(string section, string key, string value)

        {

            if (File.Exists(SettingFilePath))

            {

                long OpStation = WritePrivateProfileString(section, key, value, SettingFilePath);



                if (OpStation == 0)

                {

                    return false;

                }

                else

                {

                    return true;

                }

            }

            else

            {

                return false;

            }

        }



        // 

        private string windowState;

        public WindowState WindowState

        {

            get

            {

                if (string.IsNullOrWhiteSpace(windowState))

                {

                    SettingHelper st = SettingHelper.GetInstance();



                    windowState = st.ReadIniData("WPF", "WindowState");

                }



                if (string.IsNullOrWhiteSpace(windowState))

                {

                    windowState = ((int)System.Windows.WindowState.Normal).ToString();

                }



                return (WindowState)Convert.ToInt32(windowState);

            }



            set

            {

                windowState = ((int)value).ToString();



                SettingHelper st = SettingHelper.GetInstance(); //SettingHelper.GetInstance   SettingHelper 



                st.WriteIniData("WPF", "WindowState", windowState);

            }

        }





        // 

        private string left;

        public double Left

        {

            get

            {

                if (string.IsNullOrWhiteSpace(left)) //IsNullOrWhiteSpace 

                {

                    SettingHelper st = SettingHelper.GetInstance();



                    left = st.ReadIniData("WPF", "Left");

                }



                if (string.IsNullOrWhiteSpace(left))

                {

                    left = "20";

                }



                return Convert.ToDouble(left);

            }



            set

            {

                left = value.ToString();



                SettingHelper st = SettingHelper.GetInstance();



                st.WriteIniData("WPF", "Left", left);

            }

        }



        private string top;



        public double Top

        {

            get

            {

                if (string.IsNullOrWhiteSpace(top))

                {

                    SettingHelper st = SettingHelper.GetInstance();



                    top = st.ReadIniData("WPF", "Top");

                }



                if (string.IsNullOrWhiteSpace(top))

                {

                    top = "20";

                }



                return Convert.ToDouble(top);

            }



            set

            {

                top = value.ToString();



                SettingHelper st = SettingHelper.GetInstance();



                st.WriteIniData("WPF", "Top", top);

            }

        }

    }