winform 选择打印机功能(功能点:存取app.config节点)

 

窗体:

              winform 选择打印机功能(功能点:存取app.config节点)_第1张图片

app.comfig:         

  <appSettings>
    <add key="PrinterD" value=""/>
    <add key="PrinterX" value=""/>
    <add key="SelDX" value="D"/>
  </appSettings> 

 

后台:

        private void frmSelPrinter_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {

              //加载计算机所有打印机
 
               this.cbPrintD.Items.Add(PrinterSettings.InstalledPrinters[i]);
                this.cbPrintX.Items.Add(PrinterSettings.InstalledPrinters[i]);
            }

            //读取config节点

            string printerD =  ConfigurationManager.AppSettings["PrinterD"];
            string printerX =  ConfigurationManager.AppSettings["PrinterX"];
            string SelDX = ConfigurationManager.AppSettings["SelDX"];
            this.cbPrintD.Text = printerD;
            this.cbPrintX.Text = printerX;
            if (SelDX.Trim() == "D")
            {
                this.rdD.Checked = true;
            }
            else
            {
                this.rdX.Checked = true;
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.cbPrintD.Text))
            {
                MessageBox.Show("请选择大票打印机!");
                this.cbPrintD.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(this.cbPrintX.Text))
            {
                MessageBox.Show("请选择小票打印机!");
                this.cbPrintX.Focus();
                return;
            }
            else
            {

                try
                {

                    //更改config节点值
 
                   Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    AppSettingsSection app = config.AppSettings;
                    app.Settings["PrinterD"].Value = this.cbPrintD.Text.Trim();
                    app.Settings["PrinterX"].Value = this.cbPrintX.Text.Trim();
                    if (this.rdD.Checked)
                    {
                        app.Settings["SelDX"].Value = "D";
                    }
                    else
                    {
                        app.Settings["SelDX"].Value = "X";
                    }
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                    MessageBox.Show("保存成功!");
                    this.Close();
                }
                catch (Exception ex)
                {

                    MessageBox.Show("保存失败,原因:" + ex.Message);
                }
            }
        }

        private void rdD_CheckedChanged(object sender, EventArgs e)
        {
            this.rdD.Checked = !this.rdX.Checked;
        }

        private void rdX_CheckedChanged(object sender, EventArgs e)
        {
            this.rdD.Checked = !this.rdX.Checked;
        }

你可能感兴趣的:(exception,object,String,WinForm)