2017-09-25 96 views
1

案例:我正在創建一個讀取plc值的程序。爲了讀取這些值,將填充配置選項卡。此配置選項卡將所有值都放入一個數組列表中。如果程序關閉,它會丟失所有的配置數據,我想做一個功能來保存這個配置數據。我對這些事情完全陌生,如果你們可能有答案和/或示例代碼,我很好奇。我正在考慮存儲完整的數組列表,並且當我打開這個.whatever文件時讀出來。保存值供以後使用

正如你可能看到的,ive已經創建了一個菜單。

Arraylist name = allData。

ArrayList在一個名爲DataPerLabel的對象中接受這些數據。

配置選項卡:

Configuration tab

類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 FIL

現在我已經試過這:

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上,因爲它沒有沒有參數的構造函數。

希望有人能幫助,

感謝

+1

你應該看看保存文件到的app.config或用戶配置文件使用[ConfigurationManager](https://msdn.microsoft.com/de-de/library/system.configuration.configurationmanager(v = vs.110).aspx) –

+0

讓我看看,因爲我說im完全新和學生,所以,無論如何,謝謝! – NielsSchutte

+0

將整個數組列表存儲在文件中很容易使用序列化完成,並在啓動時使用反序列化重新加載。但我寧願使用@MarkusDeibel建議將數據存儲在配置文件中。如果配置選項卡中的數據對於每個用戶都不相同,那麼我會將這些數據存儲爲用戶設置,如果這些數據對於我將它們存儲在配置文件中的每個用戶都是相同的話。 – elloco999

回答

0

這樣!

使用:

https://www.newtonsoft.com/json

存儲列表:

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); 
+0

我需要什麼類型的文件來存儲json對象?還是自動創建一個? string path = @「C:\\ Users \\ Gebruiker \\ Desktop \\ WindowsHMI \\」; 將無法​​正常工作? – NielsSchutte

+0

它會創建一個txt文件,你應該指定路徑\\ filename –

0

您可以將數據保存爲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,您可以使用List<DataPerLabel>代替。

+0

加載我的表單時,如何將所有這些數據放入同一個arraylist allData? – NielsSchutte

+0

既然你的列表只包含一個對象類型,那麼我認爲最好是用戶List <>而不是ArrayList。 –

相關問題