I'm a month or so into my coding journey and I'm currently writing my first program. It allows the saving and organization of code snippets into an easily searched library. Screenshot
I am currently using two lists to store the data (one for entry name, and other for entry contents), which I've been able to work out saving and loading to a text file by adding a tag to EACH detail line saved. Even as a novice, this seems clunky. I've decided to try the use of verbatim strings to save a multi line textbox as a single line string. I just can't seem to figure it out though, as it seems to save each line to a new line in the text file, which has broken my load file function. Is my assumption that verbatim strings would be the way to go in this instance, or am I missing something entirely?
public void SaveCurrentLibrary() {
SaveFileDialog saveDialog = new SaveFileDialog(); //instantiates a new dialog box
saveDialog.Filter = "Code Locker File (*.cll)|*.cll|All files (*.*)|*.*";
saveDialog.InitialDirectory = appDir;
//ConvertDetailsForSaving(); commented out for testing
saveDialog.ShowDialog();
var fileName = saveDialog.FileName;
MessageBox.Show(fileName);
System.IO.File.WriteAllLines(fileName, listOfEntries);
System.IO.File.AppendAllLines(fileName, listOfDetails);
}
private void LoadLibrary() {
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "Code Locker Files (*.cll)|*.cll|All files (*.*)|*.*";
openDialog.InitialDirectory = appDir;
openDialog.ShowDialog();
var fileName = openDialog.FileName;
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(fileName)) {
string line;
while ((line = r.ReadLine()) != null) {
if(line.StartsWith("[")) {
listOfDetails.Add(line);
} else {
listOfEntries.Add(line);
lstEntries.Items.Add(line);
}
}
}
}
//Function on AddEntry form to send data to main window
private void SendDataToMain() {
main.lstEntries.Items.Add(txtName.Text);
main.listOfEntries.Add(txtName.Text);
main.listOfDetails.Add(@"[" + txtName.Text + "]" + txtContents.Text);
main.lstEntries.SelectedIndex = 0;
this.Close();
}