Beispiel #1
0
 public void Save(bool silent = false)
 {
     try
     {
         if (silent && mWatcher != null)
         {
             mWatcher.Changed            -= mFileChangedHandler;
             mWatcher.Created            -= mFileChangedHandler;
             mWatcher.EnableRaisingEvents = false;
         }
         ShouldNotify = false;
         IniWriter writer = new IniWriter();
         writer.FileName = FileName;
         writer.Serialize(Data);
         writer.Save();
         writer.Close();
     }
     catch (Exception ee)
     {
     }
     finally
     {
         ShouldNotify = true;
         if (silent && mWatcher != null)
         {
             mWatcher.Changed            += mFileChangedHandler;
             mWatcher.Created            += mFileChangedHandler;
             mWatcher.EnableRaisingEvents = false;
         }
     }
 }
        void ret_OKClicked(object sender, EventArgs e)
        {
            Dictionary <String, Object> givenValue = new Dictionary <string, object>();
            IniWriter writer = IniWriter.Open(this.TargetFileName);

            foreach (Control c in DataFieldControl)
            {
                String fullName = (String)c.Tag;
                IniReader.OnSerializeNotificationEventArgs arg = FieldDeserializeMap[fullName];
                Object value = null;

                bool textBased = false;
                if (c is TextBox)
                {
                    value     = ((TextBox)c).Text;
                    textBased = true;
                }
                else if (c is ComboBox)
                {
                    value     = ((ComboBox)c).Text;
                    textBased = true;
                }
                if (textBased)
                {
                    if (arg.Field.FieldType == typeof(int))
                    {
                        int ival = default(int);
                        int.TryParse((String)value, out ival);
                        value = ival;
                    }
                    else if (arg.Field.FieldType == typeof(double))
                    {
                        double ival = default(double);
                        double.TryParse((String)value, out ival);
                        value = ival;
                    }
                    else if (arg.Field.FieldType == typeof(bool))
                    {
                        bool ival = default(bool);
                        bool.TryParse((String)value, out ival);
                        value = ival;
                    }
                    else if (arg.Field.FieldType == typeof(Size))
                    {
                        var  list = IniReader.IntListFromString((String)value);
                        Size sz   = new Size();
                        if (list.Count == 2)
                        {
                            sz.Width  = list[0];
                            sz.Height = list[1];
                        }
                        value = sz;
                    }
                    else if (arg.Field.FieldType.IsEnum)
                    {
                        try
                        {
                            String sval    = (String)value.ToString();
                            object enumVal = Enum.Parse(arg.Field.FieldType, sval);
                            value = sval;
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else if (arg.Field.FieldType == typeof(Point))
                    {
                        var   list = IniReader.IntListFromString((String)value);
                        Point sz   = new Point();
                        if (list.Count == 2)
                        {
                            sz.X = list[0];
                            sz.Y = list[1];
                        }
                        value = sz;
                    }
                    else if (arg.Field.FieldType == typeof(Rectangle))
                    {
                        var       list = IniReader.IntListFromString((String)value);
                        Rectangle sz   = new Rectangle();
                        if (list.Count == 4)
                        {
                            sz.X      = list[0];
                            sz.Y      = list[1];
                            sz.Width  = list[2];
                            sz.Height = list[3];
                        }
                        value = sz;
                    }
                    else if (arg.Field.FieldType == typeof(int[]))
                    {
                        var list = IniReader.IntListFromString((String)value);
                        value = list;
                    }
                }
                else
                {
                    value = c.BackColor;
                }
                givenValue[fullName] = value;
            }

            writer.GivenValue = givenValue;
            writer.Serialize(this.Value);
            writer.Close();
        }