using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace project{ class Program { static void Main(string[] args) { int line = 2; ConsoleKeyInfo key; Console.WriteLine("Choose action to do(ESC - exit):\n"); Console.WriteLine("Show files from directory *"); Console.WriteLine("Write data to the stock file"); Console.SetCursorPosition(32, 2); do { key = Console.ReadKey(true); switch (key.Key) { case ConsoleKey.UpArrow: Console.SetCursorPosition(32, line); Console.Write(" "); if (line == 2) line = 3; else line--; Console.SetCursorPosition(32, line); Console.Write("*"); break; case ConsoleKey.DownArrow: Console.SetCursorPosition(32, line); Console.Write(" "); if (line == 3) line = 2; else line++; Console.SetCursorPosition(32, line); Console.Write("*"); break; case ConsoleKey.Escape: Environment.Exit(0); break; } } while (key.Key != ConsoleKey.Enter); Console.Clear(); if (line == 2) showDir(); else Write(); Console.ReadKey(); } public static void showDir() { Console.WriteLine(@"Enter the pass to the directory:(Example -> D:\)"); string filePass = Console.ReadLine(); //folder pass if (System.IO.Directory.Exists(filePass)) // Does folder exist? { string[] fileNames = System.IO.Directory.GetFiles(filePass); //getting names of files Console.WriteLine("Directory: {0} contains {1} file(s)", filePass, fileNames.Length); for (int i = 0; i < fileNames.Length; i++) { if (System.IO.File.Exists(fileNames[i])) { string name = ""; FileInfo fi = new FileInfo(fileNames[i]); DateTime cr_date; string date = ""; name = fileNames[i].Substring(filePass.Length); cr_date = System.IO.File.GetCreationTime(fileNames[i]); //creation date date = cr_date.Day + "." + cr_date.Month + "." + cr_date.Year; Console.WriteLine("{0,30} \t{1,10} byte(s) \t {2}", name, fi.Length, date); } } } else Console.Write("Pass doesn`t exist"); } public static void Write() { string fileName = "stockDetails.txt"; Console.WriteLine(@"Please enter the directory of the stockDetails.txt:"); string pass = Console.ReadLine(); //folder pass if (!Directory.Exists(pass)) { Console.WriteLine("Directory doesn`t exist!"); return; } FileStream fs; //object to write data if (!File.Exists(pass + fileName)) //File exist? { Console.Write(pass + fileName + " doesn`t exist. We will create it.\n"); fs = new FileStream(pass + fileName, FileMode.Create, FileAccess.Write); //Create file for writing } else { fs = new FileStream(pass + fileName, FileMode.Open, FileAccess.Write); Console.WriteLine(fileName + " Successfully opened");// Just open it } string Data = ""; Console.WriteLine("Please enter some data to write into the file:"); Data = Console.ReadLine();//Getting any input information from console Byte[] info = new UTF8Encoding(true).GetBytes(Data + "\r\n"); //Convert your data to Bytes fs.Seek(0, SeekOrigin.End); //Seeking for the last point in the file fs.Write(info, 0, info.Length); // Writing data fs.Close();//Do not forget to close a file Console.WriteLine("Successfully done"); } }}
Comments
Leave a comment