コード例 #1
0
        /// <summary>
        /// 現在のセクション情報をクリアして、ファイルを読み込む。
        /// ファイルが存在しなければなにもしない。
        /// </summary>
        /// <param name="filePath"></param>
        public virtual void Read(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }

            RemoveAll();

            if (File.Exists(filePath))
            {
                StreamReader            sr  = null;
                CSPrivateProfileSection sec = null;
                string        text          = null;
                List <string> comments      = new List <string>();

                try {
                    sr = new StreamReader(filePath, Encoding.Default);

                    while ((text = sr.ReadLine()) != null)
                    {
                        if (IsComment(text))
                        {
                            comments.Add(text.Substring(1));
                        }
                        else if (IsSpace(text))
                        {
                            comments.Add(null);
                        }
                        else if (IsSection(text))
                        {
                            string name = text.Substring(1, text.Length - 2);
                            sec = new CSPrivateProfileSection(name, comments);
                            sections.Add(sec);

                            comments.Clear();
                        }
                        else if (sec != null)
                        {
                            int token = text.IndexOf('=');
                            if (token >= 0)
                            {
                                CSPrivateProfileKeyValue kv =
                                    new CSPrivateProfileKeyValue(text.Substring(0, token), text.Substring(token + 1), comments);

                                comments.Clear();

                                sec.Add(kv);
                            }
                        }
                    }
                }
                finally {
                    if (sr != null)
                    {
                        sr.Close();
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// セクションを追加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public void Add(CSPrivateProfileSection obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            sections.Add(obj);
        }
コード例 #3
0
        /// <summary>
        /// 指定したキーを持つセクションを取得
        /// </summary>
        public CSPrivateProfileSection this[string key] {
            get {
                CSPrivateProfileSection sec = sections.Find(delegate(CSPrivateProfileSection s) {
                    return(s.Name == key);
                });

                if (sec == null)
                {
                    sec = new CSPrivateProfileSection(key);
                    sections.Add(sec);
                }
                return(sec);
            }
        }
コード例 #4
0
        /// <summary>
        /// 指定したセクションとキーに設定されているstring値を取得
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="def"></param>
        /// <returns></returns>
        protected string GetValueInternal(string section, string key)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (!sections.ContainsSection(section))
            {
                return(null);
            }

            CSPrivateProfileSection sec = sections[section];

            return(sec.ContainsKey(key) ? sec[key] : null);
        }
コード例 #5
0
        /// <summary>
        /// 指定したセクションの指定したキーに値を設定
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetValue(string section, string key, object value)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            CSPrivateProfileSection sec = sections[section];

            if (value != null)
            {
                TypeConverter con = TypeDescriptor.GetConverter(value);
                sec[key] = con.ConvertToString(value);
            }
            else
            {
                sec[key] = null;
            }
        }