/// <summary> /// 保存配置文件 /// </summary> private void SaveConfig() { List <string> aliasList = new List <string>(); foreach (var item in listboxAlias.Items) { aliasList.Add(item.ToString()); } var mOpcJson = new OpcJson() { HostIP = strHostIP, HostName = strHostName, ServerName = cmbServerName.SelectedItem.ToString(), UpdateRate = Convert.ToInt32(ConfigInfo.UpdateRate), AliasList = aliasList }; var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigInfo.OpcJson); using (StreamWriter file = File.CreateText(filePath)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, mOpcJson); } }
/// <summary> /// 初始化 /// </summary> private void Init() { var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigInfo.OpcJson); //判断Opc-配置文件是否存在 if (File.Exists(filePath)) { this.rtxtRecord.Text = ""; this.rtxtRecordDb.Text = ""; // deserialize JSON directly from a file using (StreamReader file = File.OpenText(filePath)) { JsonSerializer serializer = new JsonSerializer(); mOpcJson = (OpcJson)serializer.Deserialize(file, typeof(OpcJson)); } txtHostIP.Text = mOpcJson.HostIP; txtHostName.Text = mOpcJson.HostName; txtServerName.Text = mOpcJson.ServerName; listboxAlias.Items.Clear(); foreach (var item in mOpcJson.AliasList) { listboxAlias.Items.Add(item); } if (ConnectServer()) { btnReadDb.Enabled = true; btnWriteDb.Enabled = true; btnWrite.Enabled = true; } } else { MessageBox.Show("Opc-配置不存在,请先配置!", "提示信息"); var opcConfig = new OpcConfig(); opcConfig.ShowDialog(); Init(); } }