private void btnSave_Click(object sender, EventArgs e) { try { InfoArr info = new InfoArr(); foreach (ListViewItem item in lsvBlockSet.Items)//从lsvBlockSet内读取砖块信息,并存入info内 { info.Add(item.SubItems[0].Text, item.SubItems[1].Text); } config.Info = info;//把info赋给config对象的ifno属性 config.DownKey = (Keys)txtDown.Tag; config.DropKey = (Keys)txtDrop.Tag; config.MoveLeftKey = (Keys)txtLeft.Tag; config.MoveRightKey = (Keys)txtRight.Tag; config.DeasilRotateKey = (Keys)txtDeasil.Tag; config.ContraRotateKey = (Keys)txtContra.Tag; config.CoorWidth = int.Parse(txtCoorWidth.Text); config.CoorHeight = int.Parse(txtCoorHeight.Text); config.RectPix = int.Parse(txtRectPix.Text); config.BackColor = lblBackColor.BackColor; config.SaveToXmlFile();//保存成xml文件 MessageBox.Show("保存成功"); } catch { MessageBox.Show("保存失败"); } }
public void LoadFromXmlFile()//从xml读取信息 { XmlTextReader reader; if (File.Exists("BlockSet.xml")) { //有线读取外部的BlockSet.xml文件并把信息写入BlockInfo内 reader = new XmlTextReader("BlockSet.xml"); } else { //如果BlockSet.xml文件不存在,则从嵌入资源内读取BlockSet.xml Assembly asm = Assembly.GetExecutingAssembly(); Stream sm = asm.GetManifestResourceStream("Tetris.BlockSet.xml"); reader = new XmlTextReader(sm); } string key = ""; try { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "ID") { key = reader.ReadElementString().Trim(); info.Add(key, ""); } else if (reader.Name == "Color") { info[key] = reader.ReadElementString().Trim(); } else if (reader.Name == "DownKey") { _downKey = (Keys)Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "DropKey") { _dropKey = (Keys)Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "MoveLeftKey") { _moveLeftKey = (Keys)Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "MoveRightKey") { _moveRightKey = (Keys)Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "DeasilRotateKey") { _deasilRotateKey = (Keys)Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "CountRotateKey") { _contraRotateKey = (Keys)Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "CoorWidth") { _coorWidth = Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "CoorHeight") { _coorHeight = Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "RectPix") { _rectPix = Convert.ToInt32(reader.ReadElementString().Trim()); } else if (reader.Name == "BackColor") { _backColor = Color.FromArgb(Convert.ToInt32(reader.ReadElementString().Trim())); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (reader != null) { reader.Close(); } } }