0

I need to make an application that configures a holographic printer. The screen and layout are fixed now i need to configure it. I need a way that i can save all filled in data so i can load it days after it. But i cant find a way to do that

code behind

private void Save_as_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "Text file (*.txt)|*.txt|C# file (*.cs)|*.cs";
    if (saveFileDialog.ShowDialog() == true)
        File.WriteAllText(saveFileDialog.FileName, TBSOMS.Text);
}   

Need to save filled content of. Combobox, TextBox, CheckBox and Buttons to a file.

ASh
  • 34,632
  • 9
  • 60
  • 82
  • What's wrong with your code? – SᴇM Aug 30 '19 at 12:14
  • Well i have multiple TextBoxes, ComboBoxes, CheckBoxes and Buttons. and right now it only saves the first TextBox – Tycho Welten Aug 30 '19 at 12:19
  • Easier way to do it would be to get an object ex: `Configuration` out of your inputs and serialize it, then just deserialize it whenever you need it [something like this](https://stackoverflow.com/questions/4123590/serialize-an-object-to-xml) – Marian Simonca Aug 30 '19 at 12:20

1 Answers1

0

Add this code in your Save_as_Click method, you can get the file path using SaveFileDialog.

First step, create an object from your inputs

var config = new MyConfiguration();
config.Data1 = myCombobox.SelectedValue;
config.Data2 = myTextBox.Text;
config.Data3 = myCheckBox.IsChecked;

Then serialize this object using XMLSerializer

XmlSerializer serializer = new XmlSerializer(typeof(MyConfiguration));

using (StreamWriter writer = new StreamWriter(path)) // provide a path where you want to save your file
{
    serializer.Serialize(writer, config);
}

Having that file you can use it whenever you want. Just deserialize it and you have your MyConfiguration object.

Examples of XML Serialization

How to serialize an object to XML by using Visual C#

Related StackOverflow questions:

Serialize an object to XML

Save file - xmlSerializer

Marian Simonca
  • 1,472
  • 1
  • 16
  • 29
  • Have fixed the problem on another way, ```SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Text file (*.txt)"; if (sfd.ShowDialog() == true) { using (StreamWriter write = new StreamWriter(File.Create(sfd.FileName))) { write.WriteLine(TBSOMS.Text); write.WriteLine(TBWVB.Text); write.WriteLine(TBWNB.Text); write.WriteLine(TBASPMM1.Text);``` – Tycho Welten Sep 02 '19 at 09:10