国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

序列化和反序列化XML應(yīng)用程序設(shè)置類

2019-11-18 17:18:53
字體:
供稿:網(wǎng)友

  1        public class applicationSettings
  2        {
  3
  4            PRivate bool appSettingsChanged;
  5            // 用于存儲(chǔ)應(yīng)用程序設(shè)置的變量。
  6
  7            private Point formLocation;
  8       
  9            public Point FormLocation
 10            {
 11                get { return formLocation; }
 12                set
 13                {
 14                    if (value != formLocation)
 15                    {
 16                        formLocation = value;
 17                        appSettingsChanged = true;
 18                    }
 19                }
 20            }
 21
 22
 23            // 從配置文件中反序列化類。
 24            public bool LoadAppSettings()
 25            {
 26                xmlSerializer mySerializer = null;
 27                FileStream myFileStream = null;
 28                bool fileExists = false;
 29
 30                try
 31                {
 32                    // 為 ApplicationSettings 類型創(chuàng)建 XmlSerializer。
 33                    mySerializer = new XmlSerializer(typeof(ApplicationSettings));
 34                    FileInfo fi = new FileInfo(Application.LocalUserAppDataPath
 35                       + @"/myApplication.config");
 36                    // 如果配置文件存在,將其打開。
 37                    if (fi.Exists)
 38                    {
 39                        myFileStream = fi.OpenRead();
 40                        // 反序列化配置文件以創(chuàng)建新的
 41                        // ApplicationSettings 實(shí)例。
 42                        ApplicationSettings myAppSettings =
 43                          (ApplicationSettings)mySerializer.Deserialize(
 44                           myFileStream);
 45                        // 為 ApplicationSettings 類的這一實(shí)例
 46                        // 分配屬性值。
 47                        this.formLocation = myAppSettings.FormLocation;
 48                        fileExists = true;
 49                    }
 50                }
 51                catch (Exception ex)
 52                {
 53                    MessageBox.Show(ex.Message);
 54                }
 55                finally
 56                {
 57                    // 如果 FileStream 是打開的,將其關(guān)閉。
 58                    if (myFileStream != null)
 59                    {
 60                        myFileStream.Close();
 61                    }
 62                }
 63
 64           
 65                return fileExists;
 66            }
 67
 68            // 如果設(shè)置發(fā)生變化,則將
 69            // 類序列化到配置文件中。
 70            public bool SaveAppSettings()
 71            {
 72                if (this.appSettingsChanged)
 73                {
 74                    StreamWriter myWriter = null;
 75                    XmlSerializer mySerializer = null;
 76                    try
 77                    {
 78                        // 為 ApplicationSettings 類型
 79                        // 創(chuàng)建 XmlSerializer。
 80                        mySerializer = new XmlSerializer(
 81                          typeof(ApplicationSettings));
 82                        myWriter =
 83                          new StreamWriter(Application.LocalUserAppDataPath
 84                          + @"/myApplication.config", false);
 85                        // 將 ApplicationSettings 類的這一實(shí)例
 86                        // 序列化到配置文件中。
 87                        mySerializer.Serialize(myWriter, this);
 88                    }
 89                    catch (Exception ex)
 90                    {
 91                        MessageBox.Show(ex.Message);
 92                    }
 93                    finally
 94                    {
 95                        // 如果 FileStream 是打開的,將其關(guān)閉。
 96                        if (myWriter != null)
 97                        {
 98                            myWriter.Close();
 99                        }
100                    }
101                }
102                return appSettingsChanged;
103            }
104        }

出處:ting BLOG


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海南省| 常德市| 河南省| 平舆县| 汾阳市| 宁津县| 大石桥市| 松桃| 保亭| 徐水县| 宜良县| 兴文县| 定边县| 茂名市| 罗江县| 宝兴县| 上高县| 宿迁市| 甘泉县| 龙州县| 河津市| 南宁市| 开鲁县| 沈丘县| 民乐县| 泰兴市| 莱州市| 保靖县| 衡阳县| 鲁甸县| 日土县| 遵义县| 项城市| 汨罗市| 逊克县| 富川| 山西省| 巫溪县| 平江县| 娄底市| 瑞丽市|