保存值供以后使用
Saving values for later use
案例:我正在创建一个从 plc 中读取值的程序。为了读取这些值,填写了一个配置选项卡。这个配置选项卡将他所有的值放入一个数组列表中。如果程序关闭,它会丢失他所有的配置数据,我想做一个函数来保留这些配置数据。我对这些事情完全陌生,我很好奇你们是否有答案 and/or 示例代码。我正在考虑存储完整的数组列表,并在我打开这个 .whatever 文件时读出。
如您所见,我已经创建了一个菜单。
数组列表名称 = allData.
arraylists 在名为 DataPerLabel 的对象中接受此数据。
配置选项卡:
Class DataPerLabel:
class DataPerLabel
{
public String labelName;
public String labelAdress;
public String dataType;
public bool monitor;
public DataPerLabel(String labelName, String labelAdress, String dataType, bool monitor)
{
this.labelName = labelName;
this.labelAdress = labelAdress;
this.dataType = dataType;
this.monitor = monitor;
}
public String getLabelName()
{
return labelName;
}
public String getLabelAdress()
{
return labelAdress;
}
public String getDataType()
{
return dataType;
}
public bool getMonitor()
{
return monitor;
}
}
我想将此数组列表存储在 .ini 或 .txt 文件中
我已经试过了:
private void menuItemSave_Click(object sender, System.EventArgs e)
{
string yourFilePath = @"C:\Users\Gebruiker\Desktop\WindowsHMI\";
XmlSerializer serializer = new XmlSerializer(typeof(DataPerLabel));
foreach (DataPerLabel configRecord in allData)
{
using (XmlWriter writer = XmlWriter.Create(yourFilePath, new XmlWriterSettings() { Indent = true }))
{
serializer.Serialize(writer, configRecord);
}
}
}
给我错误:无法在 allDataPerLabel 上进行序列化,因为它没有不带参数的构造函数。
希望大家帮忙,
谢谢
您可以将数据保存为 xml 字符串:
List<DataPerLabel> _dataPerLabelList; // this is your data object.
.
.
.
// on saving:
XmlSerializer serializer = new XmlSerializer(typeof(List<DataPerLabel>));
using (XmlWriter writer = XmlWriter.Create(yourFilePath, new XmlWriterSettings() { Indent = true }))
{
serializer.Serialize(writer, _dataPerLabelList);
}
.
.
.
// on loading your form:
XmlSerializer deserializer = new XmlSerializer(typeof(List<DataPerLabel>));
using (XmlReader reader = XmlReader.Create(yourFilePath))
{
_dataPerLabelList = (List<DataPerLabel>)deserializer.Deserialize(reader);
}
注意:既然你的列表包含了相同类型的对象那么我认为没有必要使用ArrayList
,你可以直接使用[=12] =] 代替。
这边!
使用:
存储列表:
List<DataPerLabel> lst = new List<DataPerLabel>();
string json = JsonConvert.SerializeObject(lst);
File.WriteAllText("path", json);
阅读列表:
string json = File.ReadAllText("path");
List<DataPerLabel> lst = JsonConvert.DeserializeObject<List<DataPerLabel>>(json);
案例:我正在创建一个从 plc 中读取值的程序。为了读取这些值,填写了一个配置选项卡。这个配置选项卡将他所有的值放入一个数组列表中。如果程序关闭,它会丢失他所有的配置数据,我想做一个函数来保留这些配置数据。我对这些事情完全陌生,我很好奇你们是否有答案 and/or 示例代码。我正在考虑存储完整的数组列表,并在我打开这个 .whatever 文件时读出。
如您所见,我已经创建了一个菜单。
数组列表名称 = allData.
arraylists 在名为 DataPerLabel 的对象中接受此数据。
配置选项卡:
Class DataPerLabel:
class DataPerLabel
{
public String labelName;
public String labelAdress;
public String dataType;
public bool monitor;
public DataPerLabel(String labelName, String labelAdress, String dataType, bool monitor)
{
this.labelName = labelName;
this.labelAdress = labelAdress;
this.dataType = dataType;
this.monitor = monitor;
}
public String getLabelName()
{
return labelName;
}
public String getLabelAdress()
{
return labelAdress;
}
public String getDataType()
{
return dataType;
}
public bool getMonitor()
{
return monitor;
}
}
我想将此数组列表存储在 .ini 或 .txt 文件中
我已经试过了:
private void menuItemSave_Click(object sender, System.EventArgs e)
{
string yourFilePath = @"C:\Users\Gebruiker\Desktop\WindowsHMI\";
XmlSerializer serializer = new XmlSerializer(typeof(DataPerLabel));
foreach (DataPerLabel configRecord in allData)
{
using (XmlWriter writer = XmlWriter.Create(yourFilePath, new XmlWriterSettings() { Indent = true }))
{
serializer.Serialize(writer, configRecord);
}
}
}
给我错误:无法在 allDataPerLabel 上进行序列化,因为它没有不带参数的构造函数。
希望大家帮忙,
谢谢
您可以将数据保存为 xml 字符串:
List<DataPerLabel> _dataPerLabelList; // this is your data object.
.
.
.
// on saving:
XmlSerializer serializer = new XmlSerializer(typeof(List<DataPerLabel>));
using (XmlWriter writer = XmlWriter.Create(yourFilePath, new XmlWriterSettings() { Indent = true }))
{
serializer.Serialize(writer, _dataPerLabelList);
}
.
.
.
// on loading your form:
XmlSerializer deserializer = new XmlSerializer(typeof(List<DataPerLabel>));
using (XmlReader reader = XmlReader.Create(yourFilePath))
{
_dataPerLabelList = (List<DataPerLabel>)deserializer.Deserialize(reader);
}
注意:既然你的列表包含了相同类型的对象那么我认为没有必要使用ArrayList
,你可以直接使用[=12] =] 代替。
这边!
使用:
存储列表:
List<DataPerLabel> lst = new List<DataPerLabel>();
string json = JsonConvert.SerializeObject(lst);
File.WriteAllText("path", json);
阅读列表:
string json = File.ReadAllText("path");
List<DataPerLabel> lst = JsonConvert.DeserializeObject<List<DataPerLabel>>(json);