将Xml文件的名称保存到组合框中,然后通过单击名称将其打开

时间:2018-10-19 14:10:48

标签: c# combobox save

我想将Xml文件的名称保存到组合框中,然后通过单击与SaveFileDialogOpenFileDialog类似的已保存设置的名称来打开它。

这是我的代码的一部分:

private void btnSave_Click(object sender, EventArgs e)
    {

        SaveFileDialog file = new SaveFileDialog();
        file.Filter = "XML File |*.xml";
        if (file.ShowDialog() == DialogResult.OK)
        {
            XmlSerializer ser = new XmlSerializer(typeof(ActionsEntry));
            ActionsEntry tmpAction = new ActionsEntry();
            List<ActionsEntryAction> tmpActionsEntryActions = new List<ActionsEntryAction>();
            foreach (ListViewItem lvi in lvActions.Items)
            {
                ActionEntry tmpActionEntry = lvi.Tag as ActionEntry;
                ActionsEntryAction tmpActionsEntryAction = new ActionsEntryAction();
                tmpActionsEntryAction.X = tmpActionEntry.X;
                tmpActionsEntryAction.Y = tmpActionEntry.Y;
                tmpActionsEntryAction.Text = tmpActionEntry.Text;
                tmpActionsEntryAction.interval = tmpActionEntry.Interval;
                tmpActionsEntryAction.Type = (int)tmpActionEntry.Type;
                tmpActionsEntryActions.Add(tmpActionsEntryAction);
            }
            tmpAction.Action = tmpActionsEntryActions.ToArray();

            using (XmlWriter writer = XmlWriter.Create(file.FileName))
            {
                ser.Serialize(writer, tmpAction);
            }
        }
    }

    private void btnOpen_Click(object sender, EventArgs e)
    {

        bool runIt = false;
        if (MessageBox.Show("After openning configuration, are you want to run it?", "Click BOT", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
             == DialogResult.Yes)
        {
            runIt = true;
        }
        OpenFileDialog file = new OpenFileDialog();
        file.Filter = "XML File |*.xml";
        file.Multiselect = false;
        if (file.ShowDialog() == DialogResult.OK)
        {
            OpenFileXml(runIt, file.FileName);
            string name = file.SafeFileName;
            this.Text = "Click BOT - " + name.Substring(0, name.Length - 4);
        }
    }

    private void OpenFileXml(bool runIt, string file)
    {
        //Get data from XML file
        XmlSerializer ser = new XmlSerializer(typeof(ActionsEntry));
        using (FileStream fs = System.IO.File.Open(file, FileMode.Open))
        {
            try
            {
                ActionsEntry entry = (ActionsEntry)ser.Deserialize(fs);
                lvActions.Items.Clear();
                foreach (ActionsEntryAction ae in entry.Action)
                {
                    string point = ae.X.ToString() + "," + ae.Y.ToString();
                    string interval = (ae.interval).ToString();
                    ListViewItem lvi = new ListViewItem(new string[] { point, ((ClickType)(ae.Type)).ToString(), interval, ae.Text });
                    ActionEntry acion = new ActionEntry(ae.X, ae.Y, ae.Text, ae.interval, (ClickType)(ae.Type));
                    lvi.Tag = acion;
                    lvActions.Items.Add(lvi);
                }

                if (runIt)
                {
                    btnStart.PerformClick();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Click BOT", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

0 个答案:

没有答案