Saving/loading a verbatim string to a text file

702 Views Asked by At

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();
    }
1

There are 1 best solutions below

3
Keith Nicholas On BEST ANSWER

You can save it as lines, and your specific problem is you need to escape your strings yourself, using something like Can I convert a C# string value to an escaped string literal

But you will find it tricky long term, potentially a better way is to store it as json using Json.Net

public class Snippet
{
    public string Code { get; set; }
    public string Name { get; set; }
}

// saving loading
var snippets = new List<Snippet>()
{
    new Snippet(){Name="Saving", Code = "//Code to save\n//to Disk"},
    new Snippet(){Name="Loaidng", Code = "//Code to load\n//from Disk"}
};
var json =  JsonConvert.SerializeObject(snippets);
File.WriteAllText("snippets.json", json);

var loadedSnippets = JsonConvert.DeserializeObject<List<Snippet>>(File.ReadAllText("snippets.json"));
loadedSnippets.ForEach(s => Console.WriteLine(s.Name));

This means you model your snippet as a concept, it will then be easy to add other properties like "language" or timestamps when snippet was made, or other things.

it will save it to a file that will look like

[{"Code":"//Code to save\n//to Disk","Name":"Saving"},{"Code":"//Code to load\n//from Disk","Name":"Loaidng"}]