chciałem wykonać zapamiętywanie ustawień aplikacji w formie zapisywania obiektu klasy Ustawienia do pliku, problem w tym, że jedno z pól klasy a konkretnie SerialPort nie zostało przewidziane do serializacji, jak to obejść?
[Serializable]
public class Ustawienia {
private SerialPort bluetoothSerialPort;
public Ustawienia()
{ this.bluetoothSerialPort = new SerialPort("COM1",9600,Parity.None,8,StopBits.One);
//2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400 } = 0% @ 11,0592MHz
}
public void setBluetoothSerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
{ this.bluetoothSerialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits); }
public SerialPort getBluetoothSerialPort()
{ return this.bluetoothSerialPort; }
}
...
private void btnSerialize_Click(object sender, EventArgs e)
{
try
{
fs = new FileStream("ustawienia_save", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
ustawienia.setBluetoothSerialPort("COM8", 4800, Parity.None, 8, StopBits.One);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, ustawienia);
fs.Close();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
komunikat:Type 'System.IO.Ports.SerialPort' in Assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.