进入您的项目属性的设置选项卡并添加您想要保留的值的设置,如下所示:
然后像这样在您的代码中访问它们。当您的应用程序关闭时保存它们:
public partial class Form1 : Form
{
SerialPort serialPort;
public Form1()
{
InitializeComponent();
serialPort = new SerialPort();
serialPort.PortName = Properties.Settings.Default.PortName;
serialPort.BaudRate = Properties.Settings.Default.BaudRate;
serialPort.DataBits = Properties.Settings.Default.DataBits;
serialPort.DtrEnable = Properties.Settings.Default.DtrEnable;
serialPort.StopBits = Properties.Settings.Default.StopBits;
serialPort.Parity = Properties.Settings.Default.Parity;
}
private void button1_Click(object sender, EventArgs e)
{
serialPort.PortName = "COM1";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.PortName = serialPort.PortName;
Properties.Settings.Default.BaudRate = serialPort.BaudRate;
Properties.Settings.Default.DataBits = serialPort.DataBits;
Properties.Settings.Default.DtrEnable = serialPort.DtrEnable;
Properties.Settings.Default.StopBits = serialPort.StopBits;
Properties.Settings.Default.Parity = serialPort.Parity;
Properties.Settings.Default.Save(); //Saves settings
}
}