我的软件可以让用户设置运行周期,如图。
用户通过comboBox(不在图中)选择一个唯一的频率,图中右边显示了这个comboBox的集合:“天”“周”等。选择好后出现与之对应的panel,从上到下与右边的集合对应,而“每次运行”无对应的panel。用户设置好后我要把此信息保存在内存中,请问有什么好的方法可以保存这样的数据?

解决方案 »

  1.   

    最后一个panel里是dateTimePicker。
      

  2.   

    hashtable? datatable? xml? 总之随意了,只要好查询就行了吧?
      

  3.   

    各位,我问的是保存在“内存”中,可以在运行时读取。用ini、xml也不都要读到内存中吗?在内存中怎么保存?
      

  4.   

    不管内存中是什么东西,你都想办法把它序列化成直观的东西,保存。
    -----------------------------我不这样认为,在这个应用中,不仅仅是把这些数据保存,关键是要能够被很好地识别并且执行,分别建立几个Table进行保存即可
      

  5.   

    [code=C#]internal interface INotifier
        {
            void Serialize(XmlElement element);
            void Deserialize(XmlElement element);
            bool Enabled { get;set;}
            event EventHandler Notify;
        }    internal class NotifiySerializer
        {
            public static void Serialize(XmlElement element, INotifier notifier)
            {
                XmlDocument xmlDoc = element.OwnerDocument;
                Type t = notifier.GetType();
                XmlAttribute attr;
                if (element.HasAttribute("Assembly"))
                {
                    attr = element.Attributes["Assembly"];
                }
                else
                {
                    attr = xmlDoc.CreateAttribute("Assembly");
                    element.Attributes.Append(attr);
                }
                attr.Value = t.Assembly.FullName;
                if (element.HasAttribute("Type"))
                {
                    attr = element.Attributes["Type"];
                }
                else
                {
                    xmlDoc.CreateAttribute("Type");
                    element.Attributes.Append(attr);
                }
                attr.Value = t.FullName;
                notifier.Serialize(element);
            }        public static INotifier Deserialize(XmlElement element)
            {
                XmlDocument xmlDoc = element.OwnerDocument;
                Assembly assembly = Assembly.LoadFrom(element.Attributes["Assembly"].Value);
                INotifier notifier = assembly.CreateInstance(element.Attributes["Type"].Value) as INotifier;
                notifier.Deserialize(element);
                return notifier;
            }
        }    internal class DayNotifier : Timer, INotifier
        {
            private int cycleDay;
            private DateTime lastDateTime;        public DayNotifier()
            {
                cycleDay = 0;
            }        public DayNotifier(IContainer container)
                : base(container)
            {
                cycleDay = 0;
            }        public int CycleDay
            {
                get { return cycleDay; }
                set
                {
                    cycleDay = value;
                    lastDateTime = DateTime.Now;
                    if (cycleDay <= 0) Enabled = false;
                }
            }        protected override void OnTick(EventArgs e)
            {
                base.OnTick(e);
                if (Notify != null && lastDateTime.AddDays(cycleDay) < DateTime.Now)
                    Notify(this, EventArgs.Empty);
            }        #region INotifier 成员        public void Serialize(XmlElement element)
            {
                XmlDocument xmlDoc = element.OwnerDocument;
                XmlElement elem;
                elem = element.SelectSingleNode("LastDateTime") as XmlElement;
                if (elem == null)
                {
                    elem = xmlDoc.CreateElement("LastDateTime");
                    element.AppendChild(elem);
                }
                elem.Value = lastDateTime.ToString();
                elem = element.SelectSingleNode("CycleDay") as XmlElement;
                if (elem == null)
                {
                    elem = xmlDoc.CreateElement("CycleDay");
                    element.AppendChild(elem);
                }
                elem.Value = cycleDay.ToString();
            }        public void Deserialize(XmlElement element)
            {
                cycleDay = int.Parse(element.SelectSingleNode("CycleDay").Value);
                lastDateTime = DateTime.Parse(element.SelectSingleNode("LastDateTime").Value);
            }        public override bool Enabled
            {
                get { return base.Enabled; }
                set { base.Enabled = value; }
            }        public event EventHandler Notify;        #endregion
        }code]
    针对每天的,我写了一个示范给你,你可以在此基础上扩展为每个工作日或者每个周末通知
    你还可以自己另外写个类,实现INotifier接口,进行通知。
    使用的时候不可以不必关心具体类型,直接使用Enabled=true,以及Notify事件就行了
    序列化的话,使用NotifiySerializer提供的静态方法,它会自动处理相应类型
    代码未经调试,可能有错误
      

  6.   

    没贴好郁闷,能不能修改的啊?
    internal   interface   INotifier 
            { 
                    void   Serialize(XmlElement   element); 
                    void   Deserialize(XmlElement   element); 
                    bool   Enabled   {   get;set;} 
                    event   EventHandler   Notify; 
            }         internal   class   NotifiySerializer 
            { 
                    public   static   void   Serialize(XmlElement   element,   INotifier   notifier) 
                    { 
                            XmlDocument   xmlDoc   =   element.OwnerDocument; 
                            Type   t   =   notifier.GetType(); 
                            XmlAttribute   attr; 
                            if   (element.HasAttribute("Assembly")) 
                            { 
                                    attr   =   element.Attributes["Assembly"]; 
                            } 
                            else 
                            { 
                                    attr   =   xmlDoc.CreateAttribute("Assembly"); 
                                    element.Attributes.Append(attr); 
                            } 
                            attr.Value   =   t.Assembly.FullName; 
                            if   (element.HasAttribute("Type")) 
                            { 
                                    attr   =   element.Attributes["Type"]; 
                            } 
                            else 
                            { 
                                    xmlDoc.CreateAttribute("Type"); 
                                    element.Attributes.Append(attr); 
                            } 
                            attr.Value   =   t.FullName; 
                            notifier.Serialize(element); 
                    }                 public   static   INotifier   Deserialize(XmlElement   element) 
                    { 
                            XmlDocument   xmlDoc   =   element.OwnerDocument; 
                            Assembly   assembly   =   Assembly.LoadFrom(element.Attributes["Assembly"].Value); 
                            INotifier   notifier   =   assembly.CreateInstance(element.Attributes["Type"].Value)   as   INotifier; 
                            notifier.Deserialize(element); 
                            return   notifier; 
                    } 
            }         internal   class   DayNotifier   :   Timer,   INotifier 
            { 
                    private   int   cycleDay; 
                    private   DateTime   lastDateTime;                 public   DayNotifier() 
                    { 
                            cycleDay   =   0; 
                    }                 public   DayNotifier(IContainer   container) 
                            :   base(container) 
                    { 
                            cycleDay   =   0; 
                    }                 public   int   CycleDay 
                    { 
                            get   {   return   cycleDay;   } 
                            set 
                            { 
                                    cycleDay   =   value; 
                                    lastDateTime   =   DateTime.Now; 
                                    if   (cycleDay   <=   0)   Enabled   =   false; 
                            } 
                    }                 protected   override   void   OnTick(EventArgs   e) 
                    { 
                            base.OnTick(e); 
                            if   (Notify   !=   null   &&   lastDateTime.AddDays(cycleDay)   <   DateTime.Now) 
                                    Notify(this,   EventArgs.Empty); 
                    }                 #region   INotifier   成员                 public   void   Serialize(XmlElement   element) 
                    { 
                            XmlDocument   xmlDoc   =   element.OwnerDocument; 
                            XmlElement   elem; 
                            elem   =   element.SelectSingleNode("LastDateTime")   as   XmlElement; 
                            if   (elem   ==   null) 
                            { 
                                    elem   =   xmlDoc.CreateElement("LastDateTime"); 
                                    element.AppendChild(elem); 
                            } 
                            elem.Value   =   lastDateTime.ToString(); 
                            elem   =   element.SelectSingleNode("CycleDay")   as   XmlElement; 
                            if   (elem   ==   null) 
                            { 
                                    elem   =   xmlDoc.CreateElement("CycleDay"); 
                                    element.AppendChild(elem); 
                            } 
                            elem.Value   =   cycleDay.ToString(); 
                    }                 public   void   Deserialize(XmlElement   element) 
                    { 
                            cycleDay   =   int.Parse(element.SelectSingleNode("CycleDay").Value); 
                            lastDateTime   =   DateTime.Parse(element.SelectSingleNode("LastDateTime").Value); 
                    }                 public   override   bool   Enabled 
                    { 
                            get   {   return   base.Enabled;   } 
                            set   {   base.Enabled   =   value;   } 
                    }                 public   event   EventHandler   Notify;                 #endregion 
            }
      

  7.   

    楼上很好很强大!十分感谢!
    小弟我新学C#,楼上应该是在用xml,我看不懂类
      

  8.   

    拿着中文的msdn搜下就行了
    创建一个XmlDocument
    然后加一个XmlElement的节点在它下面
    然后调用那序列化方法就把东西存这个节点下了
    然后XmlDocument存到一个文件就行了弄回来可以参考参考其实主要给你演示了继承和接口的使用方法
      

  9.   

    如果lz想配置可读,就用xml format,否则用binary format