コード例 #1
0
        static void Main(string[] args)
        {
            string fileName   = "texta.txt";
            var    fileEditor = new FileEditor(fileName);

            fileEditor.List();
            Console.WriteLine(fileEditor.LineCount());

            Console.WriteLine("Hello to File Editor! Here is info about the comands you can use." + "\nlist - lists the contents of the file \nclear - clears the contents of the file " +
                              "\nappendline - appends a new line to the file \nappend <text> - appends the text to the file \nlinecount - outputs the numbers of lines in the file");

            //if you try to write new data to a document who does'nt exist, after entering the new data the document will be created
            Console.Write("Enter youre choice: ");
            string choice = Console.ReadLine();

            switch (choice)
            {
            case "list":
                fileEditor.List();
                break;

            case "clear":
                fileEditor.Clear();
                break;

            case "appendline":
                fileEditor.AppendLine();
                break;

            case "append <text>":
                fileEditor.AppendText();
                break;

            case "linecount":
                fileEditor.LineCount();
                break;

            default:
                Console.WriteLine("Incorect choice!");
                break;
            }
        }
コード例 #2
0
        /// <summary>
        /// Main method.
        /// </summary>
        public static void Main()
        {
            // TODO : separate logic
            // TODO : documentation
            // TODO : validation
            // TODO : TDD
            char[] splitChars = new[] { ' ', '.', '!', '?', ':', '\n', '"' };

            // string filePath = @"C:\Users\Viktor\Desktop\GitHub\HackBulgaria-CSharp\Week 8\TEST\test.txt";
            Console.Write("Enter file path: ");
            string filePath = Console.ReadLine();
            FileEditor fe1 = new FileEditor(filePath);

            // if (!File.Exists(filePath))
            // {
            //    throw new ArgumentNullException("File with such name does not exist!");
            // }
            do
            {
                Console.Write("Enter action:");
                string input = Console.ReadLine();
                string[] commands = input.Split(splitChars);

                switch (commands[0])
                {
                    case "list":
                        Console.WriteLine(fe1.List());
                        break;
                    case "clear":
                        fe1.Clear();
                        Console.WriteLine("SUCCES: File content cleared.");
                        break;
                    case "append":
                        StringBuilder appendText = new StringBuilder();
                        for (int i = 1; i < commands.Length; i++)
                        {
                            appendText.Append(string.Format(commands[i] + " "));
                        }

                        fe1.Append(appendText.ToString());
                        break;
                    case "appendline":
                    case "appendLine":
                        fe1.AppendLine();
                        Console.WriteLine("SUCCES: Added a new line.");
                        break;
                    case "linecount":
                    case "lineCount":
                        Console.WriteLine(string.Format("Number of lines :{0}", fe1.LinesCount()));
                        break;
                    case "exit":
                    case "quit":
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("ERROR:Invalid command!");
                        break;
                }
            }
            while (true);
        }