C# Create A Simple Notes Application

the Introduction

This is a simple notes application based on a command line interface, so there is no direct GUI. For beginners, this application will show you how to read, write and edit simple XML files, write to special directories and delete files from directories.

Create App

Create a new console application and name it NotesCommandLine. Since the application is command line based there won’t be much to it except a few methods. In the Program.cs file at the using directives add the following namespaces:

Sponsored Links
using System.Xml;
using System.Xml.Linq;
using System.Diagnostics;

Next, add this variable just below the Main() method:

private static string NoteDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Notes\";

This variable returns our default MyDocuments folder and the Notes folder (which might not exist yet). Yours might be different than the default location of C:\users\user\My Documents.

Next, copy the following method (you’ll get some errors but don’t worry about them):

      private static void ReadCommand()
      {

           Console.Write(Directory.GetDirectoryRoot(NoteDirectory));
           string Command = Console.ReadLine();

           switch (Command.ToLower())
           {

               case "new":
                   NewNote();
                   Main(null);
                   break;
               case "edit":
                   EditNote();
                   Main(null);
                   break;
               case "read":
                   ReadNote();
                   Main(null);
                   break;
               case "delete":
                   DeleteNote();
                   Main(null);
                   break;
               case "shownotes":
                   ShowNotes();
                   Main(null);
                   break;
               case "dir":
                   NotesDirectory();
                   Main(null);
                   break;
               case "cls":
                   Console.Clear();
                   Main(null);
                   break;
               case "exit":
                   Exit();
                   break;
               default:
                   CommandsAvailable();
                   Main(null);
                   break;
           }
       }

This method will do the following: it will read the user input and then call the appropriate method. Then once it's finished it will go back to the main method so the program will be in a continuous loop. The methods do not exist yet so right click on them and Click Generate > Method Sub.

Before we begin in the Main() method add this:

            ReadCommand();
            Console.ReadLine();

NewNote()

For the sake of brevity we won’t explain all the methods, as the majority do the same thing with a few differences. This method uses the XMLWriter to write an XML file and store the user's input.

           Console.WriteLine("Please Enter Note:\n");
           string input = Console.ReadLine();

           XmlWriterSettings NoteSettings = new XmlWriterSettings();

           NoteSettings.CheckCharacters = false;
           NoteSettings.ConformanceLevel = ConformanceLevel.Auto;
           NoteSettings.Indent = true;

           string FileName = DateTime.Now.ToString("dd-MM-yy") + ".xml";

           using (XmlWriter NewNote = XmlWriter.Create(NoteDirectory + FileName, NoteSettings))
           {

               NewNote.WriteStartDocument();
               NewNote.WriteStartElement("Note");
               NewNote.WriteElementString("body", input);
               NewNote.WriteEndElement();

               NewNote.Flush();
               NewNote.Close();
           }

EditNote()

This is similar to the above method, except with added validation to ensure the directory and files exist, and then we open the file for editing.

Console.WriteLine("Please enter file name.\n");

           string FileName = Console.ReadLine().ToLower();

           if (File.Exists(NoteDirectory + FileName))
           {

               XmlDocument doc = new XmlDocument();

               try
               {

                   doc.Load(NoteDirectory + FileName);
                   Console.Write(doc.SelectSingleNode("//body").InnerText);
                   string ReadInput = Console.ReadLine();

                   if (ReadInput.ToLower() == "cancel")

                   {

                       Main(null);

                   }

                   else

                   {

                       string newText = doc.SelectSingleNode("//body").InnerText = ReadInput;

                       doc.Save(NoteDirectory + FileName);

                   }

               }

               catch (Exception ex)

               {

                   Console.WriteLine("Could not edit note following error occurred: " + ex.Message);

               }



           }

           else

           {

               Console.WriteLine("File not found\n");

           }

DeleteNote()

Here we have more validation and we also write a confirmation message to ensure users actually want to delete the message. The reason we use a try catch is to catch any exceptions such as permission issues.

           Console.WriteLine("Please enter file name\n");

           string FileName = Console.ReadLine();


           if (File.Exists(NoteDirectory + FileName))

           {

               Console.WriteLine(Environment.NewLine + "Are you sure you wish to delete this file? Y/N\n");

               string Confirmation = Console.ReadLine().ToLower();


               if (Confirmation == "y")

               {

                   try

                   {

                       File.Delete(NoteDirectory + FileName);

                       Console.WriteLine("File has been deleted\n");

                   }

                   catch (Exception ex)

                   {

                       Console.WriteLine("File not deleted following error occured: " + ex.Message);

                   }

               }

               else if (Confirmation == "n")

               {

                   Main(null);

               }

               else

               {

                   Console.WriteLine("Invalid command\n");

                   DeleteNote();

               }

           }

           else

           {

               Console.WriteLine("File does not exist\n");

               DeleteNote();

           }

CommandsAvailable()

           Console.WriteLine(" New - Create a new note\n Edit - Edit a note\n Read -  Read a note\n ShowNotes - List all notes\n Exit - Exit the application\n Dir - Opens note directory\n Help - Shows this help message\n");

Exit()

Environment.Exit(0);

ShowNotes()

This shows all the current notes in the notes directory. If the directory doesn’t exist it will be created.

          string NoteLocation = NoteDirectory;

           DirectoryInfo Dir = new DirectoryInfo(NoteLocation);

           if (Directory.Exists(NoteLocation))
           {

               FileInfo[] NoteFiles = Dir.GetFiles("*.xml");

               if (NoteFiles.Count() != 0)
               {

                   Console.SetCursorPosition(Console.CursorLeft + 1, Console.CursorTop + 2);
                   Console.WriteLine("+------------+");
                   foreach (var item in NoteFiles)
                   {
                        Console.WriteLine("  " +item.Name);
                   }

                   Console.WriteLine(Environment.NewLine);
               }
               else
               {
                   Console.WriteLine("No notes found.\n");
               }
           }
           else
           {

               Console.WriteLine(" Directory does not exist.....creating directory\n");

               Directory.CreateDirectory(NoteLocation);

               Console.WriteLine(" Directory: " + NoteLocation + " created successfully.\n");

           }

NotesDirectory()

This method opens the notes directory.

Process.Start("explorer.exe", NoteDirectory);

Summary

This is a simple application which shows how to write, read and edit simple XML files, use the DirectoryInfo class to get a list of specific files (in this case XML) and also delete files from directories.

Feel free to expand on the project, clean it up as you see fit and perhaps add a GUI!

Download Project (NotesCommandLine.zip)