进入您的项目属性的设置选项卡,并为要坚持这样的添加值的设置:
然后访问它们在你的代码是这样的。在应用程序关闭时保存它们:
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
}
}
为什么要保存设置?你是否想过保存设置?如果你不想保存这些设置,而且它还是这样做了呢? –
您是否试图在应用程序启动之间保留设置或在运行时设置它们?您在此展示的任何内容都不会节省任何费用 –
问题不明显,请回复以前的评论提供更多信息。 –