-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
284 lines (244 loc) · 8.89 KB
/
Program.cs
File metadata and controls
284 lines (244 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.Diagnostics;
// Tutorial Link: https://www.thecodingguys.net/blog/csharp-create-command-line-notes-application
// XML Documentation: https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlwriter?view=net-6.0
namespace Note_Taking_App
{
internal class Program
{
static void Main(string[] args)
{
ReadCommand();
Console.ReadLine();
}
// Directory/Folder that holds the notes created
private static string NoteDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Notes\";
// Method that reads inputs from user: execute a certain "note action/method" based on user string
private static void ReadCommand()
{
// cd to the proper file path
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 "close":
Console.Clear();
Main(null);
break;
case "exit":
Exit();
break;
default:
CommandsAvailable();
Main(null);
break;
}
}
// Create a new note
private static void NewNote()
{
// Get Note Title
Console.WriteLine("Enter note title:\n");
string inputTitle = Console.ReadLine();
// Get Note Content
Console.WriteLine("Enter note content\n");
string inputBody = Console.ReadLine();
XmlWriterSettings NoteSettings = new XmlWriterSettings();
NoteSettings.CheckCharacters = false;
NoteSettings.ConformanceLevel = ConformanceLevel.Auto;
NoteSettings.Indent = true;
string fileName = inputTitle + ".xml";
using (XmlWriter NewNote = XmlWriter.Create(NoteDirectory + fileName, NoteSettings))
{
NewNote.WriteStartDocument();
NewNote.WriteStartElement("Note");
NewNote.WriteElementString("body", inputBody);
NewNote.WriteEndElement();
NewNote.Flush();
NewNote.Close();
}
}
// Allows user to edit specific note by specifing file name
private static void EditNote()
{
Console.WriteLine("Enter File Name:\n");
string fileName = Console.ReadLine().ToLower() + ".xml";
// Check to see if file exists
if (File.Exists(NoteDirectory + fileName))
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(NoteDirectory + fileName);
Console.Write(doc.SelectSingleNode("//body").InnerText);
string ReadInput = Console.ReadLine();
// Exit edit if user types "cancel"
if (ReadInput.ToLower() == "cancel")
{
Main(null);
}
// Write the user edit to the document
else
{
string newText = doc.SelectSingleNode("//body").InnerText = ReadInput;
doc.Save(NoteDirectory + fileName);
}
}
// If any error encountered display error message
catch (Exception ex)
{
Console.WriteLine("Could not edit note, following message occured: " + ex.Message);
}
}
// If file does not exist
else
{
Console.WriteLine("File not found\n");
}
}
// Delete a note
private static void DeleteNote()
{
Console.WriteLine("Enter File Name:\n");
string fileName = Console.ReadLine().ToLower();
// Check to see if Note exists
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);
}
// If input is neither "y" or "n"
else
{
Console.WriteLine("Invalid command\n");
DeleteNote();
}
}
else
{
Console.WriteLine("File does not exist\n");
DeleteNote();
}
}
// Open a note for user to view
private static void ReadNote()
{
Console.WriteLine("Enter File Name:\n");
string fileName = Console.ReadLine() + ".xml";
if (File.Exists(NoteDirectory + fileName))
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(NoteDirectory + fileName);
Console.Write(doc.SelectSingleNode("//body\n").InnerText);
}
catch (Exception ex)
{
Console.WriteLine("Could not read note, following message occured: " + ex.Message);
}
}
else
{
Console.WriteLine("File not found\n");
}
}
// Print all possible commands
private static void CommandsAvailable()
{
string[] commandList = new string[8];
commandList[0] = "new - Create a new note";
commandList[1] = "edit - Edit a note";
commandList[2] = "read - Read a note";
commandList[3] = "delete - Delete a note";
commandList[4] = "shownotes - List all notes";
commandList[5] = "dir - Open Note Directory";
commandList[6] = "close - Clear Console";
commandList[7] = "exit - Exit Application";
for (int i = 0; i < commandList.Length; i++)
{
Console.WriteLine(commandList[i] + "\n");
}
}
private static void ShowNotes()
{
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 Note in NoteFiles)
{
Console.WriteLine(" " + Note.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");
}
}
private static void Exit()
{
Environment.Exit(0);
}
private static void NotesDirectory()
{
Process.Start("explorer.exe", NoteDirectory);
}
}
}