1

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();
    }
DuMonYu
  • 13
  • 4
  • 1
    *"I've decided to try the use of verbatim strings to save a multi line textbox as a single line string"* That really isn't what a verbatim string is. A verbatim string (`@"some\nstring"`) is a concept in the code editor that tells the compiler to treat the following string as verbatim (not evaluating escape sequences). – Ron Beyer Apr 13 '18 at 03:41
  • @RonBeyer that's what he wants in his files. That way a multiline snippet will only take one line – Keith Nicholas Apr 13 '18 at 04:55
  • you will need to use something like https://stackoverflow.com/questions/323640/can-i-convert-a-c-sharp-string-value-to-an-escaped-string-literal – Keith Nicholas Apr 13 '18 at 04:56
  • That's exactly what I'm after Keith. I will have to give it a go after work today. Thank you. – DuMonYu Apr 13 '18 at 12:24

1 Answers1

0

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"}]
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • I have looked into Json files some, but as I'm teaching myself, there are a few concepts I haven't quite grasped as of yet. This is just a pet project to aid in my learning. I aim to change the data structure to json later once I've been able to learn more about it in depth. – DuMonYu Apr 13 '18 at 12:29
  • 1
    Decided to jump right in and it worked great. Thanks for the suggestion Keith. – DuMonYu Apr 14 '18 at 20:29
  • if you want to teach yourself even more, the next step would be to use something like sqlite (which is a database in a single file) which is what many things use in situations like these. – Keith Nicholas Apr 15 '18 at 21:16